将指向自定义对象的指针的向量存储到文件中 [英] Store a vector of pointers to custom objects to file
问题描述
我使用boost示例代码来存储文件中对象的指针的向量。
我的矢量是:
I am using the boost example code to store a vector of pointers of objects in a file. My vector is:
class VOMC{
public:
vector<State*> vomc;
...
...
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & vomc;
}
}
这会给我以下错误:
/usr/local/include/boost/serialization/access.hpp:118:9: error: ‘class State’ has no member named ‘serialize’
错误使我可能告诉我, em> State object serializable(不确定)。此外,我困惑,因为存储指针(地址到内存)不存储实际的数据,这将在程序终止时释放。上述情况有解决方法吗?即使没有提升。
The error makes is probably telling me that I should also make my State object serializable(not sure on that one). Furthermore, I am confused because storing the pointers(addresses to memory) does not store the actual data, which will be freed upon program termination. Is there a workaround for the above situation? Even without boost.
推荐答案
您需要 serialize
c $ c> State 类。
You need serialize
method for your State
class.
http://www.boost.org/doc/libs/1_51_0/libs/serialization/doc/index.html
数组停止的每个成员都将被序列化。但记住每个成员是一个指针 - 所以这是什么意思?这个序列化的整个目的是允许在另一个地方和时间重建原始数据结构。 为了用指针实现这一点,仅仅保存指针的值是不够的,而是它指向的对象必须被保存。稍后加载成员时,必须创建一个新对象,并且必须将新指针加载到类成员中。
我认为你应该阅读序列化指针
这篇关于将指向自定义对象的指针的向量存储到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!