我想序列一类具有作为指针的列表上的通用类的属性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;
?
感谢您的帮助;)
不应该'类Use'从'Base'或'GenericBase'衍生? – Inverse 2010-08-02 12:16:20
不,使用类使用基地作为一个属性 – user408535 2010-08-02 14:23:01
您所提供的使用类缺少序列化功能。 – 2010-09-06 10:50:00