c++ - 在没有默认构造函数的情况下序列化指向类的指针时无法覆盖 save_construct_data

标签 c++ pointers boost boost-serialization default-constructor

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

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]

我可以将函数定义移动到友元声明中,但这太丑陋了。

接下来我应该尝试什么?

谢谢, 贾恩

最佳答案

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

关于c++ - 在没有默认构造函数的情况下序列化指向类的指针时无法覆盖 save_construct_data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4112075/

相关文章:

c++ - c/c++中两个函数内容相同时,哪种风格更好?

c++ - 如何将模板参数与 "if"和 "switch"语句一起使用?

pointers - Julia 中的重叠数组 - 带指针的解决方案?

c++ - boost 属性树,迭代器到要插入的元素?

c++ - 将字符串变量传递给 boost::ASIO 读取器/处理程序会导致段错误

c++ - 有哪些小型、快速和轻量级的开源应用程序(µTorrent 式)?

c++ - 在 3D 中 boost 两条线段的交集

c - 为什么我必须返回指针?

指针的编译问题

c++ - 序列化所需的公共(public)空构造函数