gpt4 book ai didi

c++ - 将指向自定义对象的指针 vector 存储到文件中

转载 作者:行者123 更新时间:2023-11-30 04:25:12 28 4
gpt4 key购买 nike

我正在使用 boost 示例代码将对象指针的 vector 存储在文件中。我的载体是:

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’

错误可能是在告诉我,我还应该使我的 State 对象可序列化(不确定那个)。此外,我很困惑,因为存储指针(内存地址)并没有存储实际数据,这些数据将在程序终止时被释放。是否有针对上述情况的解决方法?即使没有 boost 。

最佳答案

您的State 类需要serialize 方法。

http://www.boost.org/doc/libs/1_51_0/libs/serialization/doc/index.html

Each member of the array stops will be serialized. But remember each member is a pointer - so what can this really mean? The whole object of this serialization is to permit reconstruction of the original data structures at another place and time. In order to accomplish this with a pointer, it is not sufficient to save the value of the pointer, rather the object it points to must be saved. When the member is later loaded, a new object has to be created and a new pointer has to be loaded into the class member.

此外,我认为您应该阅读有关 serialization of pointers 的内容

关于c++ - 将指向自定义对象的指针 vector 存储到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12283800/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com