c ++ Boost序列化,编译派生类时出错

时间:2013-02-12 10:49:05

标签: c++ serialization boost

编译这段代码时出现了一个非常奇怪的错误:

#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

编辑:

爬得更深我发现了问题,仍然没有解决方案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
    } 

1 个答案:

答案 0 :(得分:0)

基于评论。要解决此问题,您必须将save方法声明为常量。

template<typename Archive> void save(Archive& ar, const unsigned version) const {
  // ...
}

因此,save方法内部使用的所有方法也应该是常量。