2010-08-02 58 views
5

我想序列一类具有作为指针的列表上的通用类的属性c + +升压系列化序列化模板派生类

这是从哪个通用类派生的父类:

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

}; 

所以,在阅读升压序列化文档(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; 

} 

我得到一个消息,

error: 'template' (as a disambiguator) is only allowed within templates

,所以我换成了

oa.template register_type >();

通过

oa.register_type();

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

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

main(){ 

    Use u; 

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

    // load data 

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

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

    ia>>u; 

} 

丢给我一个错误:

error: no matching function for call to '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; 

感谢您的帮助;)

+0

不应该'类Use'从'Base'或'GenericBase'衍生? – Inverse 2010-08-02 12:16:20

+0

不,使用类使用基地作为一个属性 – user408535 2010-08-02 14:23:01

+0

您所提供的使用类缺少序列化功能。 – 2010-09-06 10:50:00

回答

0

but where do I have to define them ?

你可以在任何你的头的定义它们

And how do I deal with the member...

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

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

+0

但其中类haeder“你可以在任何你的头定义它们”?使用,基地,GenericBase? “我认为你可以得到提振跟踪使用BOOST_CLASS_TRACKING指针......” 有什么意义?问题不是一个单一的指针,而是一个指针图;我如何处理load_construct_data方法中的指针映射? – user408535 2010-08-02 14:28:50

+0

我把它放在一个名为“serialisation.hpp”的独立头文件中......这可能是一个好地方,然后将它包含在其他类头文件中? 地图可以处理使用内置的一些STL容器系列化的:#include“升压/系列化/ map.hpp” – user274244 2010-08-09 14:34:38

4

如果你亲的话更容易评论提供您的失败代码的工作(剪切和粘贴)的例子,与一些虚拟数据...

但我尽力回答...

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

你可以尝试声明一个受保护的默认构造函数,升压应通过接入访问。

+0

救了我的理智,增加了一个默认的构造函数的保护和所有应该在工作开始工作。 .. – mentat 2010-11-18 21:27:37