So I resolved the problem using const !
I post as it may be profitable to someone else ...
It is only needed to split the serialize function into save and load
functions and then to const_cast the Sensor
Here is the code:
#include <iostream>
#include <iterator>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <vector>
#include
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"
Newsgroups: gmane.comp.lib.boost.user To: 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
(senr, "out.txt"); loadFromFileSens (senr, "out.txt"); return 0; }
----- Original Message ----- From: "Simon Ruffieux"
Newsgroups: gmane.comp.lib.boost.user To: 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"
Newsgroups: gmane.comp.lib.boost.user To: 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