gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:00 29 4
gpt4 key购买 nike

我想将一个带有属性的类序列化为泛型类上的指针列表

这是泛型类派生的父类:

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;

}

我收到一条消息,

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

,所以我替换了

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;

}

它给我一个错误:

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 ...

但是我必须在哪里定义它们呢?在使用类的声明中?以及我如何与成员(member)打交道

map<string, Base*> BaseDatas;

?

感谢您的帮助;)

最佳答案

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

但我还是试着回答...

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

您可以尝试声明一个 protected 默认构造函数,boost 应该可以通过 access 访问它。

关于C++ Boost 序列化 序列化模板派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3386991/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com