Boost logo

Boost Users :

Subject: Re: [Boost-users] Serialization of pointers ? -->loadingnotworking...
From: Simon Ruffieux (simon.ruffieux_at_[hidden])
Date: 2010-01-15 03:54:23


Hum hum ...

The problem was that my Sensor pointer contained in the class SensorReading
was declared const:

class SensorReading{
    public:
        SensorReading(const Sensor* s=0, double time=0);
        virtual ~SensorReading();

     protected:
        double m_time;
        const Sensor* m_sensor;
};

If I do not declare it as Sensor* m_sensor; And correct accordingly the
different functions prototypes. Then it works.

However I was wondering if there were anyway of serializing it while keeping
it const ?

Thansk

----- Original Message -----
From: "Simon Ruffieux" <simon.ruffieux_at_[hidden]>
Newsgroups: gmane.comp.lib.boost.user
To: <boost-users_at_[hidden]>
Sent: Friday, January 15, 2010 9:22 AM
Subject: Re: Serialization of pointers ? -->loadingnotworking...

>I should maybe have attached with the message this more reduced code
>containing only the problematic classes.
>
> This code fails as previously said during compilation because of the
> "loadFromFileSens"
>
> CODE
>
> /*
> * SensorReading
> *
> */
> class Sensor{
> public:
> Sensor(const std::string& name="");
> virtual ~Sensor();
> inline std::string getName() const {return m_name;}
> inline void setName(const std::string& name) {m_name=name;}
>
> friend class boost::serialization::access;
> template <class Archive> void serialize(Archive& ar, const unsigned
> int version){
> ar & m_name;
> }
> protected:
> std::string m_name;
> };
> Sensor::Sensor(const std::string& name){
> m_name=name;
> }
> Sensor::~Sensor(){
> }
>
> class SensorReading{
> public:
> SensorReading(const Sensor* s=0, double time=0);
> virtual ~SensorReading();
> inline double getTime() const {return m_time;}
> inline void setTime(double t) {m_time=t;}
> inline const Sensor* getSensor() const {return m_sensor;}
> inline void print(){
> cout << "(" << m_time << "," << m_sensor->getName() << ")" <<
> endl;
> }
> friend class boost::serialization::access;
> template <class Archive> void serialize(Archive& ar, const unsigned
> int version){
> ar & m_time & m_sensor;
> }
>
> protected:
> double m_time;
> const Sensor* m_sensor;
>
> };
> SensorReading::SensorReading(const Sensor* s, double t){
> m_sensor=s;
> m_time=t;
> }
> SensorReading::~SensorReading(){}
>
>
> template <class A> void saveIntoFileSens(SensorReading& sr, const char*
> file){
> ofstream ofile(file);
> A ar(ofile);
> ar << sr;
> ofile.close();
> }
> template <class A> void loadFromFileSens(SensorReading& sr, const char*
> file){
> ifstream ifile(file);
> A ar(ifile);
> ar >> sr;
> ifile.close();
> }
>
> /*
> * MAIN
> *
> */
> int main(){
>
> Sensor s = Sensor("SensorNom");
> SensorReading senr = SensorReading(&s,67);
>
> saveIntoFileSens<text_oarchive>(senr, "out.txt");
> loadFromFileSens<text_iarchive>(senr, "out.txt");
>
> return 0;
> }
>
>
> ----- Original Message -----
> From: "Simon Ruffieux" <simon.ruffieux_at_[hidden]>
> Newsgroups: gmane.comp.lib.boost.user
> To: <boost-users_at_[hidden]>
> Sent: Friday, January 15, 2010 9:04 AM
> Subject: Re: Serialization of pointers ? --> loadingnotworking...
>
>
>> Thanks for your quick answer !
>>
>> But the orientedPoints do work do work perfectly ! (Even without
>> specifying the derived class)
>>
>> The problem is with the pointer to Sensor* contained in the SensorReading
>> class .... And it seems to me this is not a derived class ....
>>
>> I'll keep trying ...
>>
>>
>>
>> ----- Original Message -----
>> From: "Robert Ramey" <ramey_at_[hidden]>
>> Newsgroups: gmane.comp.lib.boost.user
>> To: <boost-users_at_[hidden]>
>> Sent: Thursday, January 14, 2010 5:56 PM
>> Subject: Re: Serialization of pointers ? --> loading notworking...
>>
>>
>>> replace
>>> friend class boost::serialization::access;
>>> template <class Archive> void serialize(Archive& ar, const unsigned
>>> int version){
>>> ar & this->x & this->y & theta;
>>> }
>>>
>>> with something like:
>>> friend class boost::serialization::access;
>>> template <class Archive> void serialize(Archive& ar, const unsigned
>>> int version){
>>> ar & boost::serialization::base_object<?>(*this);
>>> }
>>>
>>> Look through the documentation and samples on how to serialize derived
>>> classes.
>>>
>>> Robert Ramey


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net