如何用boost::serialization去序列化派生模板类(续)
在
如何用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...
};
依照前面几篇的列子:
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 是一个模板派生类
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 ...
}
{
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
...
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...);
}
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);
}
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);
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...);
}
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 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 ...
}
{
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 ...
}//求大神解决这个问题。。。。
{
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去序列化派生模板类(续)的更多相关文章
- boost::serialization 用基类指针转存派生类(错误多多,一波三折)
boost::serialization 也支持c++的多态,这样我们就能够通过使用基类的指针来转存派生类, 我们接着上一篇( boost::serialization(2)序列化基类 )的样例来看: ...
- 最经常使用的两种C++序列化方案的使用心得(protobuf和boost serialization)
导读 1. 什么是序列化? 2. 为什么要序列化?优点在哪里? 3. C++对象序列化的四种方法 4. 最经常使用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序猿在编写应用程序的时候往往须 ...
- 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)
导读 1. 什么是序列化? 2. 为什么要序列化?好处在哪里? 3. C++对象序列化的四种方法 4. 最常用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序员在编写应用程序的时候往往需要将 ...
- 【boost】使用serialization库序列化子类
boost.serialization库是一个非常强大又易用的序列化库,用于对象的保存与持久化等. 使用base_object可以在序列化子类的同时也序列化父类,以此获得足够的信息来从文件或网络数据中 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [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 ...
- 为什么hadoop中用到的序列化不是java的serilaziable接口去序列化而是使用Writable序列化框架
继上一个模块之后,此次分析的内容是来到了Hadoop IO相关的模块了,IO系统的模块可谓是一个比较大的模块,在Hadoop Common中的io,主要包括2个大的子模块构成,1个是以Writable ...
- boost serialization
Archive An archive is a sequence of bytes that represented serialized C++ objects. Objects can be ad ...
随机推荐
- RGB图像数据字符叠加,图像压缩(ijl库),YUV转RGB
jackyhwei 发布于 2010-01-01 12:02 点击:3218次 来自:CSDN.NET 一些非常有用的图像格式转换及使用的源代码,包括RGB图像数据字符叠加,图像压缩(ijl库),Y ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- Android 开发框架介绍
一.概述 现android开发有很多开发框架使用,做App不一定用到框架,但好框架的思想也是值得学习.选择合适的开发框架可提供实用功能,简化项目开发提升效率. 二.Afinal框架 简介 Afinal ...
- 也说Autofac在MVC的简单实践:破解在Controller构造函数中的实例化 - winhu
相信大家对Autofac并不陌生,很多人都在使用.本文只是介绍一下本人在使用时的一点想法总结. 在使用一个框架时,肯定要去它的官网查阅一下.autofac的官网给出了一些经典的使用案例.如注册容器: ...
- POJ 1064 Cable master
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37865 Accepted: 8051 Des ...
- java 使用相对路径读取文件
java 使用相对路径读取文件 1.java project环境,使用java.io用相对路径读取文件的例子: *目录结构: DecisionTree |___src ...
- Webservice 调用方式整理
前一段时间搞webservice,简单的记录了一下几种常用的调用方式,供大家参考. 第一种:Java proxy 1).用过eclipse的创建web service client来完成 2).在ec ...
- 扩展Fitnesse的ScriptTable:支持if-then
Fitnesse的ScriptTable只能顺序执行所有行,本博文介绍如何让ScriptTable支持if-then,来条件执行一行. 首先普及一下概念,什么是Fitnesse,听一听.NET版Cuc ...
- C#条件语句、循环语句
一.程序的三种结构 顺序结构 分支结构 循环结构 二.条件语句if 语句是最有用的控制结构之一. if … else …语句的语法: if (布尔表达式)执行操作的语句 或if (布尔表达式)执行操 ...
- HttpComponents 学习的两个重要文档
httpcore-tutorial-simplified-chinese httpclient-tutorial-simplified-chinese