繁体   English   中英

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

[英]C++ Boost serialization Serializing templated derived classes

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

这是泛型类派生的父类:

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 ;

};

通用类:

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; }

};

我想要序列化的类

class Use
{
    public:

        friend class boost::serialization::access;

        int Id;

        map<string, Base*> BaseDatas;

        Use();
        ~Use();

};

因此,在阅读了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;

}

我收到了一条消息,

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

,所以我换了

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;

}

它给我一个错误:

错误:没有匹配函数来调用'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 ...

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

map<string, Base*> BaseDatas;

谢谢你的帮助 ;)

如果您提供失败代码的工作(剪切和粘贴)示例,以及一些虚拟数据,则更容易发表评论......

但无论如何我试着回答......

Boost.serialisation试图调用GenericBases默认构造函数,但由于您没有提供它而失败。 Boost.serialisation首先创建您的对象(或现在尝试),然后读取文件并设置变量。

您可以尝试声明受保护的默认构造函数,该版本应该具有访问权限。

但我在哪里定义它们?

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

我该如何处理会员......

我认为你可以使用BOOST_CLASS_TRACKING获得跟踪指针的提升......

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM