c++ - 序列化boost数组

标签 c++ serialization boost xml-serialization

我想序列化一个 boost::array,其中包含一些已经可以序列化的东西。

如果出现这个错误:

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

我试图包含 serialization/array.hpp header ,但没有帮助。 是否有另一个 header 要包含?

谢谢

编辑: 删除了一个错误的链接

最佳答案

您需要显示包含在 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::list bus_stop 指针。

错误:

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

表示包含在 boost::array 中的类要么没有将 boost::serialization::access 声明为友元类,要么没有实现模板方法 serialize。

关于c++ - 序列化boost数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3045188/

相关文章:

c++ - Hadoop winultils 项目构建失败 - libwinutils.c WIDEN_STRING(x) 宏中的错误

c++ - 如何避免这种类似单例的设计模式?

json - 将可变长度的 JSON 数组解码为 Rust 数组

java - 为什么我们在序列化过程中使用 serialVersionUID

c++ - 为什么我的 WinAPI 上下文(弹出)菜单没有出现?

C++:从 ‘BaseNode*’ 到 ‘Match*’ 的无效转换

c# - MongoDB C# DateTimeOffset 序列化

c++ - Boost 序列化在特定文件大小后抛出 "input stream error"

c++ - Boost序列化不适用于shared_ptr <int>

c++ - C++ boost lambda 和 == 运算符的问题