Grievertime
Grievertime

Reputation: 13

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

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
    } 

Upvotes: 1

Views: 376

Answers (1)

megabyte1024
megabyte1024

Reputation: 8660

Basing on the comments. To fix the problem you have to declare the save method as constant.

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.

Upvotes: 1

Related Questions