C++ Boost序列化序列化模板化派生类

use*_*535 5 c++ serialization templates boost derived-class

我想序列化一个具有属性的类作为泛型类的指针列表

这是泛型类派生的父类:

class Base{

    public :

        friend class boost::serialization::access;

        virtual ~Base(){}

        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
        }

        virtual string Getid() = 0 ;

};
Run Code Online (Sandbox Code Playgroud)

通用类:

template<typename T>
class GenericBase : public Base
{
    public:

        friend class boost::serialization::access;

        GenericBase<T>(string id){}
        ~GenericBase(){}

        string id;

        vector<T> data

        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & boost::serialization::base_object<Base>(*this);
            ar & BOOST_SERIALIZATION_NVP( id);
            ar & BOOST_SERIALIZATION_NVP( data);

        }

        string Getid() { return id; }

};
Run Code Online (Sandbox Code Playgroud)

我想要序列化的类

class Use
{
    public:

        friend class boost::serialization::access;

        int Id;

        map<string, Base*> BaseDatas;

        Use();
        ~Use();

};
Run Code Online (Sandbox Code Playgroud)

因此,在阅读了boost序列化文档(http://www.boost.org/doc/libs/1_43_0/libs/serialization/doc/serialization.html#derivedpointers)之后,我在序列化代码中尝试了这个:

main(){

   Use u = Use();

   std::ofstream ofs(filename, ios::binary);

   // save data to archive

   boost::archive::binary_oarchive oa(ofs);

   oa.template register_type<GenericBase<Type1> >();
   oa.template register_type<GenericBase<Type2> >();
   oa.template register_type<GenericBase<Type3> >();

   oa<<u;

}
Run Code Online (Sandbox Code Playgroud)

我收到了一条消息,

错误:'模板'(作为消歧器)仅允许在模板中使用

,所以我换了

oa.template register_type>();

通过

oa.register_type();

它工作,我已经能够保存在文本和二进制(我检查数据)

现在加载,我只是使用这些行:

main(){

    Use u;

    std::ifstream ifs(filename, ios::binary);

    // load data

    ia.register_type<GenericBase<Type1> >();

    boost::archive::binary_iarchive ia(ifs);

    ia>>u;

}
Run Code Online (Sandbox Code Playgroud)

它给我一个错误:

错误:没有匹配函数来调用'GenericBase :: GenericBase()'

有人告诉我,我必须覆盖2个方法保存和加载,如下样本http://www.boost.org/doc/libs/1_43_0/libs/serialization/doc/serialization.html#constructors

namespace boost { namespace serialization {
template<class Archive>
inline void save_construct_data(
    Archive & ar, const my_class * t, const unsigned int file_version)
    {
        // save data required to construct instance
        ar << t->m_attribute;
    }

template<class Archive>
inline void load_construct_data(
    Archive & ar, my_class * t, const unsigned int file_version)
    {
        // retrieve data from archive required to construct new instance
        int attribute;
        ar >> attribute;
        // invoke inplace constructor to initialize instance of my_class
       ::new(t)my_class(attribute);
    }
}} // namespace ...
Run Code Online (Sandbox Code Playgroud)

但我在哪里定义它们?在使用类的声明?我该如何处理该成员

map<string, Base*> BaseDatas;
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助 ;)

小智 0

但我必须在哪里定义它们?

您可以在任何标头中定义它们

还有我要怎么跟会员打交道...

我认为你可以使用 BOOST_CLASS_TRACKING 来增强跟踪指针...

http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/special.html#objecttracking