Search code examples
c++boostboost-serialization

Store a vector of pointers to custom objects to file


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.


Solution

  • 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