序列化指向没有默认构造函数的类的指针时,无法覆盖 save_construct_data

发布于 2024-09-30 17:18:34 字数 1295 浏览 6 评论 0原文

我正在尝试遵循这个示例 http:// /www.boost.org/doc/libs/1_42_0/libs/serialization/doc/serialization.html#constructors 但我不断收到错误。按照这个例子,我在尝试访问私有变量时遇到错误(这很公平):

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

但是,如果我将 save_construct_data 添加为友元,则会出现歧义错误:

/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'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

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.

What should I try next?

Thanks,
Jayen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

像你 2024-10-07 17:18:34

save_construct_data 必须先声明,然后才能添加好友。关于处于不同命名空间中的事情。像这样:

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:

class my_class;

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

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);
}}

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

class my_class;

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

温暖的光 2024-10-07 17:18:34

确保 save_construct_data 方法没有在 boost 库调用方法的范围之外的不同范围内声明(boost::serialization)

在自定义命名空间中声明 save_construct_data 会导致歧义问题,因为 boost 不知道要调用哪个方法

Make sure the save_construct_data method isn't declared in a different scope besides that from which the boost library calls the methods (boost::serialization)

Declaring the save_construct_data in a custom namespace will cause ambiguity issues as boost won't know which method to call

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文