【问题标题】:Deserialize multiple values with boost::serialize (with SSCCE)使用 boost::serialize 反序列化多个值(使用 SSCCE)
【发布时间】:2015-03-30 00:05:14
【问题描述】:

我尝试使用 boost::serialize 库对对象进行序列化和反序列化。我需要拆分我的保存和加载功能。

我使用的库是在官方教程中描述的。我的保存和加载函数如下所示:

friend class boost::serialization::access;

template<typename Archive>
void save(Archive& ar, const unsigned version) const {
    ar & name;
    ar & NType;
    ar & NTherm;
    ar & NRun;
//...
}

template<class Archive>
void load(Archive& ar, const unsigned int version) {
    ar & name;
    ar & NType;
    ar & NTherm;
    ar & NRun;
//...
}
BOOST_SERIALIZATION_SPLIT_MEMBER()

这些函数在类的头文件中实现。我像这样序列化和反序列化: { //连载 std::ofstream ofs("output.txt"); boost::archive::text_oarchive oa(ofs); oa

{
    //Deserialize
    Class newObject;
    std::ifstream ifs("output.txt");
    boost::archive::text_iarchive ia(ifs);
    ia >> newObject;
}

序列化工作正常,但反序列化在ar &amp; NRun; 处引发异常。

弹出一条错误消息:此应用程序已请求运行时以不寻常的方式终止它。调试显示抛出异常类名太长 .

我该如何解决这个问题?

更新:在代码 sn-p 中添加了括号。

Update2:我添加了一个 SSCCE。

main.cpp

#include <iostream>
#include "simulation.h"
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/split_member.hpp>


int main()
{
    Simulation *sim;
    sim = new Simulation(2,25,25,25,100,500,1000,"Sim");
    {
        std::ofstream ofs("output.txt");
        boost::archive::text_oarchive oa(ofs);
        oa << sim;
    }

    {
        Simulation newSim;
        std::ifstream ifs("output.txt",std::ios::binary);
        boost::archive::text_iarchive ia(ifs);
        ia >> newSim;
    }
}

simulation.h

#ifndef SIMULATION_H_
#define SIMULATION_H_
#include <string>
#include <boost/serialization/access.hpp>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/string.hpp>

class Simulation {

public:
    //Constructors
Simulation(int anzType, int x=25, int y=25, int z=25, int NT = 100, int NS = 500, int NR = 1000, std::string n = "");
Simulation();           //Defaultconstructor für Boost Serialisierung

    //Destructor
    virtual ~Simulation();


private:
    int NType;             
    int NTherm;             
    int NStep;             
    int NRun;             
    std::string name;
    int Lx;
    int Ly;
    int Lz;
    int LyLz;

    friend class boost::serialization::access;

    template<typename Archive>
    void save(Archive& ar, const unsigned version) const {
        ar & name;
        ar & NType;
        ar & NTherm;
        ar & NRun;
        ar & NStep;
    }

    template<class Archive>
    void load(Archive& ar, const unsigned int version) {
        ar & name;
        ar & NType;
        ar & NTherm;
        ar & NRun;
        ar & NStep;
    }
    BOOST_SERIALIZATION_SPLIT_MEMBER()
};
#endif /* SIMULATION_H_ */

simulation.cpp

#include "Simulation.h"


Simulation::Simulation() {

}


Simulation::Simulation(int anzType, int x, int y, int z, int NT, int NS, int NR, std::string n) {
    name = n;
   NType = anzType;
   NTherm = NT;
    NStep = NS;
    NRun = NR;
    Lx = x;
    Ly = y;
    Lz = z;
    LyLz = y*z;
}

Simulation::~Simulation() {

}

【问题讨论】:

    标签: c++ serialization boost boost-serialization


    【解决方案1】:

    更新由于您使用 SSCCE 更新了问题,这很明显。

    你序列化了一个Simulation*。然后你尝试反序列化Simulation&amp;。不出所料,这行不通。

    Live On Coliru

    #include <boost/archive/text_iarchive.hpp>
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/serialization/access.hpp>
    #include <fstream>
    #include <iostream>
    
    class Simulation {
    
      public:
        // Constructors
        Simulation(int anzType, int x = 25, int y = 25, int z = 25, int NT = 100, int NS = 500, int NR = 1000, std::string n = "");
        Simulation(); // Defaultconstructor für Boost Serialisierung
    
        // Destructor
        virtual ~Simulation();
    
      private:
        std::string name;
        int         NType, NTherm, NStep, NRun;
        int         Lx, Ly, Lz, LyLz;
    
        friend class boost::serialization::access;
    
        template <typename Archive> void serialize(Archive &ar, unsigned) {
            ar & name;
            ar & NType;
            ar & NTherm;
            ar & NRun;
            ar & NStep;
        }
    };
    
    Simulation::Simulation() {}
    
    Simulation::Simulation(int anzType, int x, int y, int z, int NT, int NS, int NR, std::string n)
      : name(n), NType(anzType), NTherm(NT), NStep(NS), NRun(NR),
        Lx(x), Ly(y), Lz(z), LyLz(y * z)
    {
    }
    
    Simulation::~Simulation() {}
    
    int main() {
        Simulation *sim = new Simulation(2, 25, 25, 25, 100, 500, 1000, "Sim");
        {
            std::ofstream ofs("output.txt", std::ios::binary);
            boost::archive::text_oarchive oa(ofs);
            oa << sim;
        }
    
        {
            Simulation* newSim = nullptr;
            std::ifstream ifs("output.txt", std::ios::binary);
            boost::archive::text_iarchive ia(ifs);
            ia >> newSim;
    
            delete newSim;
        }
    }
    

    【讨论】:

    • std::ios::binary 下也没有显式刷新来解决问题。流用于程序中的单独功能。
    • 那么您需要将其设为 SSCCE。代码没问题。问题出在其他地方
    • 我刚刚看到程序似乎输出以下错误:Invalid parameter passed to C runtime function.
    • @pyman 我刚刚注意到您的更新。我将其与适当更新的答案相匹配。下次,考虑展示相关细节。 (我们没有理由假设 Object 实际上不是一个对象 :))
    • 糟糕。我没有用 C++ 编写这么长时间的代码,只是错过了它。当然很明显……
    猜你喜欢
    • 1970-01-01
    • 2015-01-22
    • 2012-09-05
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    相关资源
    最近更新 更多