当序列化指向没有默认构造函数的类的指针时,会覆盖save_construct_data [英] Trouble overriding save_construct_data when serializing a pointer to a class without a default constructor

查看:123
本文介绍了当序列化指向没有默认构造函数的类的指针时,会覆盖save_construct_data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遵循此示例 http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/serialization.html#constructors ,但我不断收到错误。按照示例,我尝试访问私有变量时遇到错误(足够好):

I'm trying to follow this example http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/serialization.html#constructors but I keep getting errors. Following the example, I get an error trying to access a private variable (fair enough):

bs.cpp:10: error: ‘const int my_class::m_attribute’ is private

但是,如果我把save_construct_data添加为朋友,我得到一个歧义错误:

But, if I add save_construct_data as a friend, I get an ambiguity error:

/usr/include/boost/serialization/serialization.hpp:148: error: call of overloaded ‘save_construct_data(boost::archive::text_oarchive&, const my_class*&, const boost::serialization::version_type&)’ is ambiguous
/usr/include/boost/serialization/serialization.hpp:83: note: candidates are: void boost::serialization::save_construct_data(Archive&, const T*, unsigned int) [with Archive = boost::archive::text_oarchive, T = my_class]
bs.cpp:10: note:                 void boost::serialization::save_construct_data(Archive&, const my_class*, unsigned int) [with Archive = boost::archive::text_oarchive]
bs.cpp:29: note:                 void boost::serialization::save_construct_data(Archive&, const my_class*, unsigned int) [with Archive = boost::archive::text_oarchive]

我可以将函数定义移动到朋友声明,但是这只是丑陋。

I can move the function definition to the friend declaration, but that's just ugly.

我应该怎么做?

谢谢,
Jayen

Thanks, Jayen

推荐答案

save_construct_data必须先声明才能加入。在不同的命名空间。像这样:

save_construct_data has to be declared before it can be friended. Something about being in a different namespace. Like this:

namespace boost { namespace serialization {
template<class Archive>
inline void save_construct_data(Archive & ar, const my_class * t, const unsigned int file_version);
}}

但是,因为这取决于my_class,所以你必须声明my_class :

But, because that depends on my_class, you have to declare my_class as well:

class my_class;

所以整个东西看起来像 http://pastebin.com/embed_iframe.php?i=aFyCpjyY

So the whole thing looks like http://pastebin.com/embed_iframe.php?i=aFyCpjyY

这篇关于当序列化指向没有默认构造函数的类的指针时,会覆盖save_construct_data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆