[英]c++ Boost serialize, error when compiling derived class
i'm getting a very strange error when compiling this piece of code: 编译这段代码时出现一个非常奇怪的错误:
#ifndef packetint_h
#define packetint_h
#include "../packet.h"
class packetInt: public packet{
public:
packetInt(int pLength);
~packetInt();
double distance(packet* destPacket);
friend class boost::serialization::access;
template<typename Archive> void save(Archive& ar, const unsigned version) {
ar << boost::serialization::base_object<const packet>( *this);
for(int i = 0; i<getLength(); i++)
ar << ((int*)data)[i];
}
template<typename Archive> void load(Archive& ar, const unsigned version) {
std::cout<<"test"<<std::endl;
ar >> boost::serialization::base_object<packet>(* this);
data = new int[getLength()];
for(int i = 0; i<getLength(); i++)
ar >> ((int*)data)[i];
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};
class packetIntGenome: public packetGenome{
public:
packetIntGenome(int pLength);
~packetIntGenome();
};
#endif
when compiling 编译时
C:\Users\griever\Progetti\Daana\trunk\dependencies\boost\boost/serialization/access.hpp(93): error C2662: 'void packetInt::save<Archive>(Archive &,const unsigned int)': impossibile convertire il puntatore 'this' da 'const packetInt' a 'packetInt &'
1> with
1> [
1> Archive=boost::archive::text_oarchive
1> ]
1> La conversione comporta la perdita dei qualificatori
i'm not sure why but removing 我不知道为什么,但删除
BOOST_SERIALIZATION_SPLIT_MEMBER()
fix the compiling error ( but the code will not work ) 修复编译错误(但是代码不起作用)
sorry for my bad english, i'm italian :D 对不起,我的英语不好,我是意大利语:D
EDIT: 编辑:
crawling deeper i have found the problem, still no solution btw 爬得更深,我发现了问题,但仍然没有解决办法
BOOST_SERIALIZATION_SPLIT_MEMBER()
template<typename Archive>
void serialize(Archive &ar,const unsigned int file_version){
boost::serialization::split_member(ar, *this, file_version); <--- this is not working
}
Basing on the comments. 根据评论。 To fix the problem you have to declare the save
method as constant. 要解决此问题,您必须将save
方法声明为常量。
template<typename Archive> void save(Archive& ar, const unsigned version) const {
// ...
}
Accordingly, all methods which are used inside of the save
method should be constant as well. 因此,在save
方法内部使用的所有方法也应保持不变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.