[英]c++ Boost serialize, error when compiling derived class
编译这段代码时出现一个非常奇怪的错误:
#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
编译时
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
我不知道为什么,但删除
BOOST_SERIALIZATION_SPLIT_MEMBER()
修复编译错误(但是代码不起作用)
对不起,我的英语不好,我是意大利语:D
编辑:
爬得更深,我发现了问题,但仍然没有解决办法
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
}
根据评论。 要解决此问题,您必须将save
方法声明为常量。
template<typename Archive> void save(Archive& ar, const unsigned version) const {
// ...
}
因此,在save
方法内部使用的所有方法也应保持不变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.