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<text_iarchive>(p2, "out.txt"); It raises the follwoing error during compilation: wheelchair@wheelchair-laptop:~/playerstage/boostTest$<wheelchair@wheelchair-laptop:%7E/playerstage/boostTest$>g++ simple_RangeReading.cpp -o simple -lboost_serialization /usr/local/include/boost/archive/detail/iserializer.hpp: In static member function ‘static void boost::archive::detail::load_pointer_type<Archive>::invoke(Archive&, Tptr&) [with Tptr = const Sensor*, Archive = boost::archive::text_iarchive]’: /usr/local/include/boost/archive/detail/iserializer.hpp:554: instantiated from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::text_iarchive, T = const Sensor*]’ /usr/local/include/boost/archive/detail/common_iarchive.hpp:61: instantiated from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = const Sensor*, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/basic_text_iarchive.hpp:62: instantiated from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&, int) [with T = const Sensor*, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/text_iarchive.hpp:66: instantiated from ‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int) [with T = const Sensor*, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/detail/interface_iarchive.hpp:61: instantiated from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = const Sensor*, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/detail/interface_iarchive.hpp:68: instantiated from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator&(T&) [with T = const Sensor*, Archive = boost::archive::text_iarchive]’ simple_RangeReading.cpp:279: instantiated from ‘void SensorReading::serialize(Archive&, unsigned int) [with Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/serialization/access.hpp:109: instantiated from ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = SensorReading]’ /usr/local/include/boost/serialization/serialization.hpp:74: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = SensorReading]’ /usr/local/include/boost/serialization/serialization.hpp:133: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = SensorReading]’ /usr/local/include/boost/archive/detail/iserializer.hpp:173: instantiated from ‘void boost::archive::detail::iserializer<Archive, T>::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::text_iarchive, T = SensorReading]’ simple_RangeReading.cpp:424: instantiated from here /usr/local/include/boost/archive/detail/iserializer.hpp:486: error: reinterpret_cast from type ‘const Sensor**’ to type ‘void**’ casts away constness /usr/local/include/boost/archive/detail/iserializer.hpp:489: error: no matching function for call to ‘boost::archive::detail::load_pointer_type<boost::archive::text_iarchive>::pointer_tweak(const boost::serialization::extended_type_info&, const Sensor*&, const Sensor&)’ /usr/local/include/boost/archive/detail/iserializer.hpp: In member function ‘void boost::archive::detail::pointer_iserializer<Archive, T>::load_object_ptr(boost::archive::detail::basic_iarchive&, void*&, unsigned int) const [with Archive = boost::archive::text_iarchive, T = const Sensor]’: simple_RangeReading.cpp:424: instantiated from here /usr/local/include/boost/archive/detail/iserializer.hpp:286: error: invalid conversion from ‘const void*’ to ‘void*’ /usr/local/include/boost/archive/detail/iserializer.hpp:294: error: invalid conversion from ‘const void*’ to ‘void*’ /usr/local/include/boost/archive/detail/iserializer.hpp:294: error: initializing argument 1 of ‘void boost::archive::detail::basic_iarchive::next_object_pointer(void*)’ /usr/local/include/boost/serialization/access.hpp: In static member function ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = const Sensor]’: /usr/local/include/boost/serialization/serialization.hpp:74: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = const Sensor]’ /usr/local/include/boost/serialization/serialization.hpp:133: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = const Sensor]’ /usr/local/include/boost/archive/detail/iserializer.hpp:173: instantiated from ‘void boost::archive::detail::iserializer<Archive, T>::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::text_iarchive, T = const Sensor]’ simple_RangeReading.cpp:424: instantiated from here /usr/local/include/boost/serialization/access.hpp:109: error: passing ‘const Sensor’ as ‘this’ argument of ‘void Sensor::serialize(Archive&, unsigned int) [with Archive = boost::archive::text_iarchive]’ discards qualifiers /usr/local/include/boost/serialization/access.hpp: In static member function ‘static void boost::serialization::access::construct(T*) [with T = const Sensor]’: /usr/local/include/boost/serialization/serialization.hpp:98: instantiated from ‘void boost::serialization::load_construct_data(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive, T = const Sensor]’ /usr/local/include/boost/serialization/serialization.hpp:163: instantiated from ‘void boost::serialization::load_construct_data_adl(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive, T = const Sensor]’ /usr/local/include/boost/archive/detail/iserializer.hpp:295: instantiated from ‘void boost::archive::detail::pointer_iserializer<Archive, T>::load_object_ptr(boost::archive::detail::basic_iarchive&, void*&, unsigned int) const [with Archive = boost::archive::text_iarchive, T = const Sensor]’ simple_RangeReading.cpp:424: instantiated from here /usr/local/include/boost/serialization/access.hpp:123: error: invalid conversion from ‘const void*’ to ‘void*’ /usr/local/include/boost/serialization/access.hpp:123: error: initializing argument 2 of ‘void* operator new(size_t, void*)’ /usr/local/include/boost/archive/detail/check.hpp: In function ‘void boost::archive::detail::check_const_loading() [with T = const Sensor]’: /usr/local/include/boost/archive/detail/iserializer.hpp:539: instantiated from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::text_iarchive, T = const Sensor]’ /usr/local/include/boost/archive/detail/common_iarchive.hpp:61: instantiated from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = const Sensor, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/basic_text_iarchive.hpp:62: instantiated from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&, int) [with T = const Sensor, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/text_iarchive.hpp:66: instantiated from ‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int) [with T = const Sensor, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/detail/interface_iarchive.hpp:61: instantiated from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = const Sensor, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/serialization/nvp.hpp:87: instantiated from ‘void boost::serialization::nvp<T>::load(Archivex&, unsigned int) [with Archivex = boost::archive::text_iarchive, T = const Sensor]’ /usr/local/include/boost/serialization/access.hpp:101: instantiated from ‘static void boost::serialization::access::member_load(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = boost::serialization::nvp<const Sensor>]’ /usr/local/include/boost/serialization/split_member.hpp:54: instantiated from ‘static void boost::serialization::detail::member_loader<Archive, T>::invoke(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = boost::serialization::nvp<const Sensor>]’ /usr/local/include/boost/serialization/split_member.hpp:69: instantiated from ‘void boost::serialization::split_member(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = boost::serialization::nvp<const Sensor>]’ /usr/local/include/boost/serialization/nvp.hpp:89: instantiated from ‘void boost::serialization::nvp<T>::serialize(Archive&, unsigned int) [with Archive = boost::archive::text_iarchive, T = const Sensor]’ /usr/local/include/boost/serialization/access.hpp:109: instantiated from ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = boost::serialization::nvp<const Sensor>]’ /usr/local/include/boost/serialization/serialization.hpp:74: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = boost::serialization::nvp<const Sensor>]’ /usr/local/include/boost/serialization/serialization.hpp:133: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = boost::serialization::nvp<const Sensor>]’ /usr/local/include/boost/archive/detail/iserializer.hpp:349: instantiated from ‘static void boost::archive::detail::load_non_pointer_type<Archive>::load_only::invoke(Archive&, const T&) [with T = boost::serialization::nvp<const Sensor>, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/detail/iserializer.hpp:415: instantiated from ‘static void boost::archive::detail::load_non_pointer_type<Archive>::invoke(Archive&, T&) [with T = const boost::serialization::nvp<const Sensor>, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/detail/iserializer.hpp:554: instantiated from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::text_iarchive, T = const boost::serialization::nvp<const Sensor>]’ /usr/local/include/boost/archive/detail/common_iarchive.hpp:61: instantiated from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = const boost::serialization::nvp<const Sensor>, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/basic_text_iarchive.hpp:62: instantiated from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&, int) [with T = const boost::serialization::nvp<const Sensor>, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/text_iarchive.hpp:66: instantiated from ‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int) [with T = const boost::serialization::nvp<const Sensor>, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/detail/interface_iarchive.hpp:61: instantiated from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = const boost::serialization::nvp<const Sensor>, Archive = boost::archive::text_iarchive]’ /usr/local/include/boost/archive/detail/iserializer.hpp:307: instantiated from ‘void boost::archive::detail::pointer_iserializer<Archive, T>::load_object_ptr(boost::archive::detail::basic_iarchive&, void*&, unsigned int) const [with Archive = boost::archive::text_iarchive, T = const Sensor]’ simple_RangeReading.cpp:424: instantiated from here /usr/local/include/boost/archive/detail/check.hpp:162: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’ /usr/local/include/boost/archive/detail/check.hpp:162: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’ wheelchair@wheelchair-laptop:~/playerstage/boostTest$<wheelchair@wheelchair-laptop:%7E/playerstage/boostTest$> Here is the code: #include <iostream> #include <iterator> #include <algorithm> #include <sstream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/string.hpp> using namespace std; using namespace boost::archive; using namespace boost::serialization; /* * point * */ template <class T> struct point{ inline point():x(0),y(0) {} inline point(T _x, T _y):x(_x),y(_y){} T x, y; friend class boost::serialization::access; template <class Archive> void serialize(Archive& ar, const unsigned int version){ ar & x & y; } }; /* * OrientedPoint * */ template <class T, class A> struct orientedpoint: public point<T>{ inline orientedpoint() : point<T>(0,0), theta(0) {}; inline orientedpoint(const point<T>& p); inline void print(){cout << "(" << this->x << "," << this->y << "," << theta << ")";} A theta; friend class boost::serialization::access; template <class Archive> void serialize(Archive& ar, const unsigned int version){ ar & this->x & this->y & theta; } }; template <class T, class A> orientedpoint<T,A>::orientedpoint(const point<T>& p){ this->x=p.x; this->y=p.y; this->theta=0.; } /* * 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(){} typedef orientedpoint<double, double> OrientedPoint; /* * Particle * */ struct Particle{ /**constructs a particle, given a map @param map: the particle map */ //Particle(const ScanMatcherMap& map); Particle(){}; /** @returns the weight of a particle */ inline operator double() const {return weight;} /** @returns the pose of a particle */ inline operator OrientedPoint() const {return pose;} /** sets the weight of a particle @param w the weight */ inline void setWeight(double w) {weight=w;} /** Display a particle */ inline void print(){ cout << endl<< "Particle: " << endl; cout << "-----------------" << endl; cout << "OrientedPoint pose: "; pose.print(); cout << endl; cout << "OrientedPoint previousPose: "; previousPose.print(); cout << endl; cout << "Weight: "<< weight << endl; cout << "WeightSum: "<< weightSum << endl; cout << "gweight: "<< gweight << endl; cout << "previousIndex: "<< previousIndex << endl; cout << "SensorReading: "; sr.print(); cout << endl; } /** The map */ //ScanMatcherMap map; /** The pose of the robot */ OrientedPoint pose; /** The pose of the robot at the previous time frame (used for computing thr odometry displacements) */ OrientedPoint previousPose; /** The weight of the particle */ double weight; /** The cumulative weight of the particle */ double weightSum; double gweight; /** The index of the previous particle in the trajectory tree */ int previousIndex; /** Entry to the trajectory tree */ //TNode* node; //Tests SensorReading sr; friend class boost::serialization::access; template <class Archive> void serialize(Archive& ar, const unsigned int version){ ar & pose & previousPose & weight & gweight & weightSum & previousIndex & sr; } }; /* * Particle * SaveToFile and LoadFromFile * */ template <class A> void saveIntoFile(Particle& particle, const char* file){ ofstream ofile(file); A ar(ofile); ar << particle; ofile.close(); } template <class A> void loadFromFile(Particle& particle, const char* file){ ifstream ifile(file); A ar(ifile); ar >> particle; ifile.close(); //return array; } /* * MAIN * */ int main(){ Particle p = Particle(); p.pose = OrientedPoint(point<double>(1.1,4.4)); p.previousPose = OrientedPoint(point<double>(2.1,8.4)); p.weight = 3; p.weightSum = 8.5; p.gweight = 12.7; p.previousIndex = 6; Sensor s= Sensor("Sensor Name"); p.sr = SensorReading(&s,35); p.print(); saveIntoFile<text_oarchive>(p, "out.txt"); Particle p2 = Particle(); loadFromFile<text_iarchive>(p2, "out.txt"); p2.print(); cout << "end" << endl; return 0; }

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" <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

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

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

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
participants (2)
-
Robert Ramey
-
Simon Ruffieux