代码之家  ›  专栏  ›  技术社区  ›  0x26res

序列化boost数组

  •  2
  • 0x26res  · 技术社区  · 15 年前

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

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

    我试图包含serialization/array.hpp头,但没有帮助。

    谢谢

    编辑:

    1 回复  |  直到 15 年前
        1
  •  1
  •   manifest    15 年前

    您需要显示boost::数组中包含的类的代码。因为boost::array STL-compliant this 例子。

    包含在boost::数组中的类必须将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容器应该能够序列化巴士站:

    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;
    

    错误:

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

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