Store a vector of pointers to custom objects to file

倾然丶 夕夏残阳落幕 提交于 2019-12-11 10:57:32

问题


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;
    }
}

This gives me the following error(among few more):

/usr/local/include/boost/serialization/access.hpp:118:9: error: ‘class State’ has no member named ‘serialize’

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.


回答1:


You need serialize method for your State class.

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.

Also i think you should read about serialization of pointers



来源:https://stackoverflow.com/questions/12283800/store-a-vector-of-pointers-to-custom-objects-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!