如何用boost::serialization去序列化派生模板类这篇文章中,介绍了序列化派生类模板类,

在写測试用例时一直出现编译错误,调了非常久也没跳出来,今天偶然试了一下...竟然调了出来。

先看看变异错误的代码(。。。看不出有错,但是编译就有错)。

基类代码:

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

依照前面几篇的列子:

#1应该声明为友元类(不知道为什么,看看这篇文章boost::serialization
拆分serialize函数
)。

#2实现一个serialize函数。这应该没问题???????但是却又问题。后面就知

道了。

然后来看看派生模板类的代码:

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 是一个模板派生类

#2 声明友元类

#3 实现派生类的serialize函数

这样写应该也没问题。

好,接下来写个save()函数測试一下:

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不能訪问私有数据。非常诡异!

serialize本来就是派生类的成员函数,并且access类还声明为友元类(access 要调用派生类的serialize)。不知道为什么?求解。。。。。。。

找不出就换了一种方法,上面的serialize函数intrusive式,就换成一种non-intrusive式的,

由于非入侵式的没有this指针:

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 ...
}

错误代码例如以下(有兴趣的调下。。。。error存档):

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去序列化派生模板类(续)的更多相关文章

  1. boost::serialization 用基类指针转存派生类(错误多多,一波三折)

    boost::serialization 也支持c++的多态,这样我们就能够通过使用基类的指针来转存派生类, 我们接着上一篇( boost::serialization(2)序列化基类 )的样例来看: ...

  2. 最经常使用的两种C++序列化方案的使用心得(protobuf和boost serialization)

    导读 1. 什么是序列化? 2. 为什么要序列化?优点在哪里? 3. C++对象序列化的四种方法 4. 最经常使用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序猿在编写应用程序的时候往往须 ...

  3. 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)

    导读 1. 什么是序列化? 2. 为什么要序列化?好处在哪里? 3. C++对象序列化的四种方法 4. 最常用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序员在编写应用程序的时候往往需要将 ...

  4. 【boost】使用serialization库序列化子类

    boost.serialization库是一个非常强大又易用的序列化库,用于对象的保存与持久化等. 使用base_object可以在序列化子类的同时也序列化父类,以此获得足够的信息来从文件或网络数据中 ...

  5. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. [LeetCode] Serialize and Deserialize N-ary Tree N叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  8. 为什么hadoop中用到的序列化不是java的serilaziable接口去序列化而是使用Writable序列化框架

    继上一个模块之后,此次分析的内容是来到了Hadoop IO相关的模块了,IO系统的模块可谓是一个比较大的模块,在Hadoop Common中的io,主要包括2个大的子模块构成,1个是以Writable ...

  9. boost serialization

    Archive An archive is a sequence of bytes that represented serialized C++ objects. Objects can be ad ...

随机推荐

  1. Oracle RAC环境下如何更新patch(Rolling Patch)

    Oracle RAC数据库环境与单实例数据库环境有很多共性,也有很多异性.对于数据库补丁的更新同样如此,都可以通过opatch来完成.但RAC环境的补丁更新有几种不同的更新方式,甚至于可以在零停机的情 ...

  2. JS三级折叠菜单特效 自动收缩其它级

    真的很不错!很实用,在IE6.IE7.IE8.FF.chrome等浏览器都正常运行,去掉CSS中 #menu ul中 {height:100px; overflow:auto;} 即可高度自适应 &l ...

  3. spring3.0.5的aop使用

    spring3.0.5开始支持jpa2.0了,但是最近笔者在使用他的的时候发现了3.0.5的包与2.5.5相比,有所精简.其他外部的包,我们需要自己下载. AOP必须的spring包 org.spri ...

  4. java解析XML四种方法

    XML现在已经成为一种通用的数据交换格式,平台的无关性使得很多场合都需要用到XML. XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便 ...

  5. esd-ESD试题

    ylbtech-doc:esd-ESD试题 ESD试题 1.A,ESD试题返回顶部 不定项选择题(下列选择题ABCD四项中至少有一项是正确的,共20小题): 1.{ESD题目}储备阶段的几个主要岗位是 ...

  6. MySql相关及如何删除MySql服务

    又会一招–如何删除MySql服务 进入“控制面板->管理工具->服务”查看才发现,虽然MYSQL已经卸载了,但是MYSQL服务仍然残留在系统服务里.又不想改服务名,改怎么办呢. 后来上百度 ...

  7. Linux+Apache+Tomcat集群配置

    参考: http://blog.csdn.net/bluishglc/article/details/6867358# http://andashu.blog.51cto.com/8673810/13 ...

  8. HDU 5278 PowMod 数论公式推导

    题意:中文题自己看吧 分析:这题分两步 第一步:利用已知公式求出k: 第二步:求出k然后使用欧拉降幂公式即可,欧拉降幂公式不需要互质(第二步就是BZOJ3884原题了) 求k的话就需要构造了(引入官方 ...

  9. HDU-3280 Equal Sum Partitions

    http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 M ...

  10. BLOCK 死循环

    __weak typeof(self) weakSelf = self; myObj.myBlock =  ^{     __strong typeof(self) strongSelf = weak ...