views:

118

answers:

1

I would like to serialize a boost::array, containing something that is already serializable.

If get this error:

error C2039: 'serialize' : is not a member of 'boost::array<T,N>'

I have tried to include the serialization/array.hpp header but it did not help. Is there another header to include ?

Thanks

EDIT: Removed a wrong link

+1  A: 

You need to show the code for the class contained in the boost::array. Since boost::array is STL-compliant, there should be no reason that this would not work. You should be doing something like the bus_route and bus_stop classes in this example.

The class contained in the boost::array must declare boost::serialization::access as a friend class and implement the serialize method as below:

class bus_stop
{
    friend class boost::serialization::access;
    friend std::ostream & operator<<(std::ostream &os, const bus_stop &gp);
    virtual std::string description() const = 0;
    gps_position latitude;
    gps_position longitude;
    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & latitude;
        ar & longitude;
    }
protected:
    bus_stop(const gps_position & _lat, const gps_position & _long) :
        latitude(_lat), longitude(_long)
    {}
public:
    bus_stop(){}
    virtual ~bus_stop(){}
};

Once that is done, a std container should be able to serialize the bus_stop:

class bus_route
{
    friend class boost::serialization::access;
    friend std::ostream & operator<<(std::ostream &os, const bus_route &br);
    typedef bus_stop * bus_stop_pointer;
    std::list<bus_stop_pointer> stops;
    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        // in this program, these classes are never serialized directly but rather
        // through a pointer to the base class bus_stop. So we need a way to be
        // sure that the archive contains information about these derived classes.
        //ar.template register_type<bus_stop_corner>();
        ar.register_type(static_cast<bus_stop_corner *>(NULL));
        //ar.template register_type<bus_stop_destination>();
        ar.register_type(static_cast<bus_stop_destination *>(NULL));
        // serialization of stl collections is already defined
        // in the header
        ar & stops;
    }
public:
    bus_route(){}
    void append(bus_stop *_bs)
    {
        stops.insert(stops.end(), _bs);
    }
};

Note the important line:

ar & stops;

Which will automatically iterate through the std container, in this case a std::list of bus_stop pointers.

The error:

error C2039: 'serialize' : is not a member of 'boost::array<T,N>'

Indicates that the class contained in the boost::array either has not declared boost::serialization::access as a friend class or has not implemented the template method serialize.

manifest