class base_class { public: base_class(int m=0) : base_member_(0) {} virtual ~base_class() {} virtual void print_data() = 0; private: class boost::serialization::access;//#1 template<typename Archive> void serialize(Archive & ar, const unsigned int file_version)//#2 { ar & BOOST_SERIALIZATION_NVP(base_member_); //ar & BOOST_SERIALIZATION_NVP(other member...); } protected: int base_member_; //other member... };
按照前面几篇的列子:
template<typename T>//#1 class divided_class : public base_class { public: divided_class(int m = 0, T d = T()) : base_class(m), diveded_member_(d) {} virtual ~divided_class() {} virtual void print_data() { std::cout << base_member_ << " " << diveded_member_ << " "; } private: class boost::serialization::access;//#2 template<typename Archive> void serialize(Archive& ar, const unsigned int file_version)//#3 { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base_class); ar & BOOST_SERIALIZATION_NVP(diveded_member_); //ar & BOOST_SERIALIZATION_NVP(other member...); } private: T diveded_member_; //other member.... };
#1 diveide_class 是一个模板派生类
void save() { std::ofstream ofs("t8.xml"); boost::archive::xml_oarchive oa(ofs); base_class* int_base = new divided_class<int>(1, 3); base_class* str_base = new divided_class<std::string>(1, "wyp"); base_class* float_base = new divided_class<float>(1, 3.1415926f); //Now the tricky point is to register class in serialize oa.template register_type<divided_class<int>>(NULL); oa.template register_type<divided_class<std::string>>(NULL); oa.template register_type<divided_class<float>>(NULL); //begin serialize oa & BOOST_SERIALIZATION_NVP(int_base); oa & BOOST_SERIALIZATION_NVP(str_base); oa & BOOST_SERIALIZATION_NVP(float_base); //delete pointer ... }在main函数中条用这个save函数。
Error 1 error C2248: ‘T88::divided_class<T>::serialize‘ : cannot access private member declared in class ‘T88::divided_class<T>‘ d:\sdk\boost_1_53_0\boost\serialization\access.hpp 118 Error 2 error C2248: ‘T88::divided_class<T>::serialize‘ : cannot access private member declared in class ‘T88::divided_class<T>‘ d:\sdk\boost_1_53_0\boost\serialization\access.hpp 118 ...就是派生类的serialize不能访问私有数据。很诡异!
template<typename Archive> void serialize(Archive& ar, const unsigned int file_version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base_class);//#1 ar & BOOST_SERIALIZATION_NVP(diveded_member_); //ar & BOOST_SERIALIZATION_NVP(other member...); }#1这个宏中包含一个this指针,因此这个只能留在类里面,就是定义一个函数条用这个宏:
template<typename Archive> void serialize_base_class(Archive& ar) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base_class); }然后声明派生模板类的友元函数serialize:
template<typename Archive, typename TT> friend void serialize(Archive& ar, divided_class<TT>& divcls, const unsigned int file_version);接着就是在类的外面实现这个函数:
template<typename Archive, typename T> void serialize(Archive& ar, divided_class<T>& discls, const unsigned int file_version) { discls.serialize_base_class(ar);//条用序列化基类的成员函数 ar & BOOST_SERIALIZATION_NVP(discls.diveded_member_); //ar & BOOST_SERIALIZATION_NVP(other member...); }这样应该解决问题了,编译。。。。
Error 2 error C2248: ‘T88::base_class::serialize‘ : cannot access private member declared in class ‘T88::base_class‘ d:\sdk\boost_1_53_0\boost\serialization\access.hpp 118只能才去同样的方法来解决:
private: /* class boost::serialization::access; template<typename Archive> void serialize(Archive & ar, const unsigned int file_version) { ar & BOOST_SERIALIZATION_NVP(base_member_); //ar & BOOST_SERIALIZATION_NVP(other member...); }* */ template<typename Archive> friend void serialize(Archive& ar, base_class& bascls, const unsigned int file_version); 在类外面: template<typename Archive> void serialize(Archive& ar, base_class& bascls, const unsigned int file_version) { ar & BOOST_SERIALIZATION_NVP(bascls.base_member_); //ar & BOOST_SERIALIZATION_NVP(other member...); }编译运行果然解决问题!
class base_class { public: base_class(int m=0) : base_member_(0) {} virtual ~base_class() {} virtual void print_data() = 0; private: template<typename Archive> friend void serialize(Archive& ar, base_class& bascls, const unsigned int file_version); protected: int base_member_; //other member... }; template<typename Archive> void serialize(Archive& ar, base_class& bascls, const unsigned int file_version) { ar & BOOST_SERIALIZATION_NVP(bascls.base_member_); //ar & BOOST_SERIALIZATION_NVP(other member...); } template<typename T> class divided_class : public base_class { public: divided_class(int m = 0, T d = T()) : base_class(m), diveded_member_(d) {} virtual ~divided_class() {} virtual void print_data() { std::cout << base_member_ << " " << diveded_member_ << " "; } private: class boost::serialization::access; template<typename Archive> void serialize_base_class(Archive& ar) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base_class); } template<typename Archive, typename TT> friend void serialize(Archive& ar, divided_class<TT>& divcls, const unsigned int file_version); private: T diveded_member_; //other member.... }; template<typename Archive, typename T> void serialize(Archive& ar, divided_class<T>& discls, const unsigned int file_version) { discls.serialize_base_class(ar); ar & BOOST_SERIALIZATION_NVP(discls.diveded_member_); //ar & BOOST_SERIALIZATION_NVP(other member...); } void save() { std::ofstream ofs("t8.xml"); boost::archive::xml_oarchive oa(ofs); base_class* int_base = new divided_class<int>(1, 3); base_class* str_base = new divided_class<std::string>(1, "wyp"); base_class* float_base = new divided_class<float>(1, 3.1415926f); //Now the tricky point is to register class in serialize oa.template register_type<divided_class<int>>(NULL); oa.template register_type<divided_class<std::string>>(NULL); oa.template register_type<divided_class<float>>(NULL); //begin serialize oa & BOOST_SERIALIZATION_NVP(int_base); oa & BOOST_SERIALIZATION_NVP(str_base); oa & BOOST_SERIALIZATION_NVP(float_base); //delete pointer ... }
class base_class { public: base_class(int m=0) : base_member_(0) {} virtual ~base_class() {} virtual void print_data() = 0; private: class boost::serialization::access; template<typename Archive> void serialize(Archive & ar, const unsigned int file_version) { ar & BOOST_SERIALIZATION_NVP(base_member_); //ar & BOOST_SERIALIZATION_NVP(other member...); } protected: int base_member_; //other member... }; template<typename T> class divided_class : public base_class { public: divided_class(int m = 0, T d = T()) : base_class(m), diveded_member_(d) {} virtual ~divided_class() {} virtual void print_data() { std::cout << base_member_ << " " << diveded_member_ << " "; } private: class boost::serialization::access; template<typename Archive> void serialize(Archive& ar, const unsigned int file_version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base_class); ar & BOOST_SERIALIZATION_NVP(diveded_member_); //ar & BOOST_SERIALIZATION_NVP(other member...); } private: T diveded_member_; //other member.... }; void save() { std::ofstream ofs("t8.xml"); boost::archive::xml_oarchive oa(ofs); base_class* int_base = new divided_class<int>(1, 3); base_class* str_base = new divided_class<std::string>(1, "wyp"); base_class* float_base = new divided_class<float>(1, 3.1415926f); //Now the tricky point is to register class in serialize oa.template register_type<divided_class<int>>(NULL); oa.template register_type<divided_class<std::string>>(NULL); oa.template register_type<divided_class<float>>(NULL); //begin serialize oa & BOOST_SERIALIZATION_NVP(int_base); oa & BOOST_SERIALIZATION_NVP(str_base); oa & BOOST_SERIALIZATION_NVP(float_base); //delete pointer ... }//求大神解决问题。。。。
怎样用boost::serialization去序列化派生模板类(续),布布扣,bubuko.com
怎样用boost::serialization去序列化派生模板类(续)
原文地址:http://blog.csdn.net/xiaoliangsky/article/details/25008661