Trouble overriding save_construct_data when serializing a pointer to a class without a default constructor

爱⌒轻易说出口 提交于 2020-01-02 08:12:32

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/4112075/trouble-overriding-save-construct-data-when-serializing-a-pointer-to-a-class-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!