序列化boost阵列

时间:2010-06-15 12:43:39

标签: c++ serialization boost xml-serialization

我想序列化一个boost :: array,其中包含已经可序列化的内容。

如果出现此错误:

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

我试图包含序列化/ array.hpp标头,但它没有帮助。 是否还有其他标题?

由于

修改 删除了错误的链接

1 个答案:

答案 0 :(得分:1)

您需要显示boost :: array中包含的类的代码。由于boost :: array是STL-compliant,因此没有理由认为这不起作用。您应该在this示例中执行类似bus_route和bus_stop类的操作。

boost :: array中包含的类必须将boost :: serialization :: access声明为友元类,并实现serialize方法,如下所示:

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(){}
};

一旦完成,std容器应该能够序列化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);
    }
};

请注意重要的一行:

ar & stops;

哪个会自动遍历std容器,在本例中是一个std :: bus_stop指针列表。

错误:

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

表示boost :: array中包含的类未将boost :: serialization :: access声明为友元类,或者未实现模板方法serialize。