序列化指向没有默认构造函数的类的指针时,无法覆盖 save_construct_data
我正在尝试遵循这个示例 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
save_construct_data 必须先声明,然后才能添加好友。关于处于不同命名空间中的事情。像这样:
但是,因为这取决于 my_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:
But, because that depends on my_class, you have to declare my_class as well:
So the whole thing looks like http://pastebin.com/embed_iframe.php?i=aFyCpjyY
确保 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