Serialization of pointers ? --> loading not working ...
Hello again !
I am continuing my structure serialization ...
Seems I do not get something with pointers ... I have followed the
"demo.cpp" example and tried to do the same as they are doing for pointers
but I get an exception when trying to relaod my structure !
Hope someone can help me ....
What I do:
-Save a Particle structure.
- this particle contains a SensoRreading class
-this sensorReading has a pointer to a Sensor class (only containing
a string)
When I save(serialize) the particle, the file "out.txt" contains the
following text:
22 serialization::archive 6 0 0 0 0 1.1000000000000001 4.4000000000000004 0
2.1000000000000001 8.4000000000000004 0 3 12.699999999999999 8.5 6 0 0 35 3
1 0 0 11 Sensor Name
This seems correct to me !
But when I try to load it back in a particle using
Particle p2 = Particle();
loadFromFile
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
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"
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
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
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
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"
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
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
participants (2)
-
Robert Ramey
-
Simon Ruffieux