Serialization newbie needs help
Hi all, I just cannot understand why the following code fails to compile. I have read the help files, did my Google, stared at it for hours, etc. But I cannot make sense. It compiles to the following message. Any ideas will be appreciated, --Hector C. ----MESSAGE---- // if your program traps here, it indicates taht your doing one of the following: // a) serializing an object of a type marked "track_never" through a pointer. // b) saving an non-const object of a type not markd "track_never) // Either of these conditions may be an indicator of an error usage of the // serialization library and should be double checked. See documentation on // object tracking. --END MESSAGE-- ----CODE---- #include <iomanip> #include <iostream> #include <fstream> #include <string> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/utility.hpp> #include <boost/serialization/list.hpp> using namespace std; class A { public: string m; A(){}; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & m; } }; int main(int argc, char * argv[]) { A a; { ofstream off("test.txt"); boost::archive::text_oarchive oa(off); //Uncommenting any of the following lines //produces compilation errors //oa << a; //oa << (const A) a; } return 0; } ---END CODE--- ---------------------------------- ____________________________________________________________________________________ Building a website is a piece of cake. Yahoo! Small Business gives you all the tools to get online. http://smallbusiness.yahoo.com/webhosting
Hi! Hector C. schrieb:
----MESSAGE---- // if your program traps here, it indicates taht your doing one of the following: // a) serializing an object of a type marked "track_never" through a pointer. // b) saving an non-const object of a type not markd "track_never) // Either of these conditions may be an indicator of an error usage of the // serialization library and should be double checked. See documentation on // object tracking. --END MESSAGE--
Read up here: http://www.boost.org/libs/serialization/doc/index.html
int main(int argc, char * argv[]) { A a;
change to: const A a;
{ ofstream off("test.txt"); boost::archive::text_oarchive oa(off); //Uncommenting any of the following lines //produces compilation errors //oa << a;
use oa << a;
//oa << (const A) a;
do not use the cast "(const A)". It's bad.
} return 0; } ---END CODE---
Frank
THANK YOU! (Shouting intended) Just for future reference, your solution lead me to: A a; { ofstream off("test.txt"); boost::archive::text_oarchive oa(off); oa << (const A &)a; } --Hector C. --- Frank Birbacher <bloodymir.crap@gmx.net> wrote:
Hi!
Hector C. schrieb:
----MESSAGE---- // if your program traps here, it indicates taht your doing one of the following: // a) serializing an object of a type marked "track_never" through a pointer. // b) saving an non-const object of a type not markd "track_never) // Either of these conditions may be an indicator of an error usage of the // serialization library and should be double checked. See documentation on // object tracking. --END MESSAGE--
Read up here:
http://www.boost.org/libs/serialization/doc/index.html
int main(int argc, char * argv[]) { A a;
change to: const A a;
{ ofstream off("test.txt"); boost::archive::text_oarchive oa(off); //Uncommenting any of the following lines //produces compilation errors //oa << a;
use oa << a;
//oa << (const A) a;
do not use the cast "(const A)". It's bad.
} return 0; } ---END CODE---
Frank
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
---------------------------------- ____________________________________________________________________________________ Sick sense of humor? Visit Yahoo! TV's Comedy with an Edge to see what's on, when. http://tv.yahoo.com/collections/222
Hi! Hector C. schrieb:
THANK YOU! (Shouting intended)
Just for future reference, your solution lead me to:
A a; { ofstream off("test.txt"); boost::archive::text_oarchive oa(off); oa << (const A &)a; }
--Hector C.
I now noticed that the link I posted does not reference excatly what I meant to point at. Your code makes me think that you have not read what I wanted you to read. Here it goes. READ THIS: http://www.boost.org/libs/serialization/doc/rationale.html#trap Frank
let me know if the following doesn't work Robert Ramey Hector C. wrote: #include <fstream> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp> //#include <string> // remove this #include <boost/serialization/string.hpp> // use this instead !!! using namespace std; class A { public: string m; A(){}; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & m; } }; int main(int argc, char * argv[]) { cosnt A a; // note "const" here { ofstream off("test.txt"); boost::archive::text_oarchive oa(off); oa << a; } return 0; }
participants (3)
-
Frank Birbacher
-
Hector C.
-
Robert Ramey