
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 <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/string.hpp> #include <boost/serialization/split_member.hpp> using namespace std; using namespace boost::archive; using namespace boost::serialization; /* * Class Sensor * */ 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;} protected: std::string m_name; private: friend class boost::serialization::access; template <class Archive> void serialize(Archive& ar, const unsigned int version){ cout << "Serialization of Sensor" << endl; ar & m_name; } }; Sensor::Sensor(const std::string& name){ m_name=name; } Sensor::~Sensor(){ } /* * Class SensorReading * */ 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 << "SensorReading: " << "(" << m_time << "," << m_sensor->getName() << ")" << endl; } protected: double m_time; const Sensor* m_sensor; private: friend class boost::serialization::access; template <class Archive> void save(Archive& ar, const unsigned int version)const{ cout << "Serialization of SensorReading" << endl; ar << m_time; ar << m_sensor; } template <class Archive> void load(Archive& ar, const unsigned int version){ cout << "Deserialization of SensorReading" << endl; ar >> m_time; ar >> const_cast<Sensor*&>(m_sensor); } template <class Archive> void serialize(Archive& ar, const unsigned int version){ boost::serialization::split_member(ar, *this, version); } }; SensorReading::SensorReading(const Sensor* s, double t){ m_sensor=s; m_time=t; } SensorReading::~SensorReading(){} /* * Class 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(){ const Sensor s = Sensor("SensorNom"); SensorReading sr = SensorReading(&s,67); sr.print(); saveIntoFileSens<text_oarchive>(sr, "out.txt"); SensorReading sr2; loadFromFileSens<text_iarchive>(sr2, "out.txt"); sr2.print(); cout << "end" << endl; return 0; } ----- Original Message ----- From: "Simon Ruffieux" <simon.ruffieux@gmail.com> Newsgroups: gmane.comp.lib.boost.user To: <boost-users@lists.boost.org> Sent: Friday, January 15, 2010 9:54 AM Subject: Re: Serialization of pointers ?-->loadingnotworking...
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@gmail.com> Newsgroups: gmane.comp.lib.boost.user To: <boost-users@lists.boost.org> 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@gmail.com> Newsgroups: gmane.comp.lib.boost.user To: <boost-users@lists.boost.org> 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@rrsd.com> Newsgroups: gmane.comp.lib.boost.user To: <boost-users@lists.boost.org> 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