2010-08-02 92 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