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; }
};
< em>我想要序列化的类
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#衍生指针),我在序列化代码中尝试了这个:
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 ...
但我必须在哪里定义它们?在 Use 类的声明中?我该如何与该会员打交道
map<string, Base*> BaseDatas;
?
感谢您的帮助 ;)
i would like to serialize a class with an attribute as a list of pointers on a generic class
This is the parent class from which the generic class derives :
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 ;
};
The generic class :
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; }
};
The class i want to serialize
class Use
{
public:
friend class boost::serialization::access;
int Id;
map<string, Base*> BaseDatas;
Use();
~Use();
};
So, after reading the boost serialization doc (http://www.boost.org/doc/libs/1_43_0/libs/serialization/doc/serialization.html#derivedpointers), i tried this in the serialization code :
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;
}
I got a message,
error: 'template' (as a disambiguator)
is only allowed within templates
, so i replaced
oa.template register_type >();
by
oa.register_type();
it worked and i have been able to save in text and in binary (i checked the datas)
for loading now, i just used these lines :
main(){
Use u;
std::ifstream ifs(filename, ios::binary);
// load data
ia.register_type<GenericBase<Type1> >();
boost::archive::binary_iarchive ia(ifs);
ia>>u;
}
it threw me an error :
error: no matching function for call to 'GenericBase::GenericBase()'
someone told me i had to override 2 methods save and load like in this sample 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 ...
but where do I have to define them ? In declaration of the Use class ? And how do I deal with the member
map<string, Base*> BaseDatas;
?
thanks for your help ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您提供失败代码的工作(剪切和粘贴)示例以及一些虚拟数据,则评论会更容易...
但我还是尝试回答...
Boost.serialization 正在尝试调用 GenericBases 默认构造函数,但由于您不提供而失败。 Boost.serialization 首先创建您的对象(或立即尝试),然后读取文件并设置变量。
您可以尝试声明一个受保护的默认构造函数,boost 应该可以通过 access 访问该构造函数。
It is easier to comment if you provide a working (cut-and-paste) example of your failing code, with some dummy data ...
But I try to answer anyway ...
Boost.serialisation is trying to call GenericBases default constructor, but fails since you don't provide it. Boost.serialisation first creates your object (or tries now), THEN reads the file and sets the variables.
You could try declaring a protected default constructor, which boost should have access to through access.
您可以在任何标头中定义它们
我认为您可以使用 BOOST_CLASS_TRACKING 来提升跟踪指针...
http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/special.html#objecttracking
You can define them in any of your headers
I think you can get boost to track pointers using BOOST_CLASS_TRACKING ...
http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/special.html#objecttracking