1

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

#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
    } 
4

1 回答 1

1

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

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

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

于 2013-02-12T12:37:11.387 回答