0

我有一段代码可以序列化文件中的结构。

int main(int argn, char* argv[]){
    if (argn > 2){
        std::cout << "Error! this program requires a single argument.\n";
        return 1;
    }
    std::string model_filename{argv[1]};
    std::string out_f = model_filename.replace(model_filename.length()-4,4,".dat");
    std::string out_file_name{out_f};
    std::ifstream is(argv[1]);
    std::string line;
    model m;
    while (std::getline(is,line))
    {// build m from input file
    }

    std::ofstream os(out_file_name);
    boost::archive::text_oarchive oa(os);
    oa << m ;
    os.close();

现在从同一台计算机上的不同程序读取序列化对象

int main(int argc, char* argv[]){
    const double stdev{0.03};

    std::string model_filename{argv[1]};
    std::ifstream is(model_filename);
    boost::archive::text_iarchive ia(is); **// <== throw exception on this line!!!**
    model m; 
    ia >> m; 
    ...

此代码在我的 macbook(clang 编译器)上按预期运行,但实际上在 pc(gcc 7.5)上崩溃。我在两种情况下都使用相同的 cmake 脚本构建代码。抛出的异常是

terminate called after throwing an instance of 'boost::archive::archive_exception'
  what():  input stream error

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

我只是想知道问题可能是什么(以及解决方案是什么样的!)有什么建议吗?

编辑:回答关于我定义一个参数类的类的评论


    class Parameter {
        paramType type;
        bool active{true};
        double value;
        friend boost::serialization::access;

        template<typename Archive>
        void serialize(Archive &ar, const unsigned int version) {
            ar & type;
            ar & active;
            ar & value;
        }

    public:
        Parameter() = default;

        Parameter(paramType type, bool isActive, double value) : type(type), active(isActive), value(value) {}

        paramType getType() const {
            return type;
        }

        bool isActive() const {
            return active;
        }

        double getValue() const {
            return value;
        }

        void setType(paramType _type) {
            Parameter::type = _type;
        }

        void setIsActive(bool isActive) {
            Parameter::active = isActive;
        }

        void setValue(double _value) {
            Parameter::value = _value;
        }

        friend std::ostream &operator<<(std::ostream &os, const Parameter &parameter) {
            os << "type: " << parameter.type << " active: " << parameter.active << " value: " << parameter.value;
            return os;
        }
    };

一组参数组成一个节点


    struct node {
        std::map<int, Parameter> params;
    public:
        node() = default;

    private:
        friend boost::serialization::access;

        template<typename Archive>
        void serialize(Archive &ar, const unsigned int version) {
            ar & params;
        }

    };

和一个 std::vector 的节点使模型


    struct model {
        model() = default;
// fields ::
        std::vector<node> nodes;

    private:
        friend boost::serialization::access;

        template<typename Archive>
        void serialize(Archive &ar, const unsigned int version) {
            ar & nodes;
        }
    };

为了完整起见,这里是模型头的包含部分

#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <map>
#include <boost/serialization/map.hpp>
#include <vector>
#include <boost/serialization/vector.hpp>
#include <boost/random.hpp>
#include <fstream>
#include <cmath>
#include <numeric>
#include <exception>
#include <algorithm>
#include <boost/histogram.hpp>
#include <boost/timer/timer.hpp>
#ifdef _OMP
#include <omp.h>
#endif
4

0 回答 0