如何使用 boost::serialization 以对象向量作为属性序列化对象
How to serialize object with vector of objects as attribute using boost::serialization
我正在尝试创建对象的基础,其中包含来自不同 class 的对象的属性向量。这是我的代码:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
using namespace std;
class form_mesto
{
public:
string mesto;
int year;
int mounth;
int day;
bool visit = false;
form_mesto(string a_mesto)
{
mesto = a_mesto;
}
};
class Place
{
private:
friend class boost::serialization::access;
template<class Archieve>
void serialize(Archieve&ar, const unsigned int version)
{
ar& mestа;
ar& person;
}
public:
string person;
vector<form_mesto> mestа;
Place(string a_person)
{
person = a_person;
}
void add_place(form_mesto a_mesto)
{
mestа.push_back(a_mesto);
}
};
int main()
{
string input_in_form = "London";
string input_in_Place = "Eugene";
form_mesto z = form_mesto(input_in_form);
Place x = Place(input_in_Place);
x.add_place(z);
std::ofstream ofs("save.dat", std::ios::binary);
boost::archive::binary_oarchive oa(ofs);
oa<<x;
};
错误,我得到的是:
c:\boost_1_57_0\boost\serialization\access.hpp(118):错误 C2039:序列化:不是 "std::vector>".
的成员
有人可以分享如何序列化该类型对象的经验吗?
要使代码可编译,必须执行以下操作:
包括 header 负责矢量序列化
#include <boost/serialization/vector.hpp>
将serialize
方法添加到form_mesto
class
class form_mesto
{
private:
friend class boost::serialization::access;
template<class Archieve>
void serialize(Archieve&ar, const unsigned int version)
{
// ...
}
// ...
};
Here为可编译代码
我正在尝试创建对象的基础,其中包含来自不同 class 的对象的属性向量。这是我的代码:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
using namespace std;
class form_mesto
{
public:
string mesto;
int year;
int mounth;
int day;
bool visit = false;
form_mesto(string a_mesto)
{
mesto = a_mesto;
}
};
class Place
{
private:
friend class boost::serialization::access;
template<class Archieve>
void serialize(Archieve&ar, const unsigned int version)
{
ar& mestа;
ar& person;
}
public:
string person;
vector<form_mesto> mestа;
Place(string a_person)
{
person = a_person;
}
void add_place(form_mesto a_mesto)
{
mestа.push_back(a_mesto);
}
};
int main()
{
string input_in_form = "London";
string input_in_Place = "Eugene";
form_mesto z = form_mesto(input_in_form);
Place x = Place(input_in_Place);
x.add_place(z);
std::ofstream ofs("save.dat", std::ios::binary);
boost::archive::binary_oarchive oa(ofs);
oa<<x;
};
错误,我得到的是:
c:\boost_1_57_0\boost\serialization\access.hpp(118):错误 C2039:序列化:不是 "std::vector>".
的成员有人可以分享如何序列化该类型对象的经验吗?
要使代码可编译,必须执行以下操作:
包括 header 负责矢量序列化
#include <boost/serialization/vector.hpp>
将
serialize
方法添加到form_mesto
classclass form_mesto { private: friend class boost::serialization::access; template<class Archieve> void serialize(Archieve&ar, const unsigned int version) { // ... } // ... };
Here为可编译代码