使用 boost::serialize (使用 SSCCE) 反序列化多个值
Deserialize multiple values with boost::serialize (with SSCCE)
我尝试使用 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 & NRun;
.
弹出一条错误消息,指出:此应用程序已请求运行时以异常方式终止它。调试显示抛出的异常类名太长。
我该如何解决这个问题?
更新:在代码片段中添加了括号。
更新2:我添加了一个SSCCE。
主.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;
}
}
模拟.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_ */
模拟.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() {
}
更新 由于您使用SSCCE更新了问题,因此很明显。
序列化Simulation*
。然后您尝试反序列化Simulation&
.不出所料,这是行不通的。
住在科里鲁
#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;
}
}
相关文章:
- Protobuf中重复字段的问题.使用重复字段进行序列化/反序列化的更好方法是什么?
- 在C++中使用POCO和grain对多个对象进行反序列化
- 使用 Json 转换器反序列化 WCF 服务中的C++字符串
- 使用 QDataStream 对原始数据进行反序列化
- 使用协议缓冲区创建通用反序列化程序
- Protobuf的反序列化功能之一可以使用Cereal重新创建吗?
- 使用 reinterpret_cast 序列化和反序列化数据
- 使用谷物序列化/反序列化 SFML 向量类
- static_assert 在使用 boost 反序列化二进制数据时失败"typex::value"失败
- 使用 sstream 中的提升binary_iarchive进行序列化和反序列化
- 使用 Cereal 反序列化 JSON 字符串
- 使用分配器对哈希映射进行高效的序列化和反序列化
- 使用 fwrite/fread 对矩阵进行二进制(反)序列化不起作用
- 在 c++ 中使用 Json 序列化/反序列化字节数组
- 无法使用 protobuf-net 在 C# 中反序列化
- 使用Boost序列化和反序列化JSON
- 如何使用boost c++反序列化对和对象的向量
- Boost:重新使用/清除text_iarchive以从Asio:receive()中反序列化数据
- C++:如何在不使用库的情况下序列化/反序列化对象
- 使用 boost::serialize (使用 SSCCE) 反序列化多个值