Need help with Boost:Serialization!

Hi, I'm very new at Boost and want to develop a project for school with the Boost:Serialization package. I've already read the documantation and serveral other sources. I'm not very good in programming C++, so please excuse that my code looks not perfect. I hope it's ok that I've posted some sourcecode. I've tried to keep it short... *My problem:* I have to create a program to serialise and deserialse a xml-file to a binary-stream. I think to binary stream is already done but the xml-serialisation is not ready and I don't know whats wrong. I have 3 classes: Goal ------------ class Goal { private: enum Scored{home=0, visitors=1}; friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & name; ar & minute; ar & shootout; ar & scored; ar & penality; } string name; int minute; bool shootout; Scored scored; bool penality; public: Goal(){}; Goal(const string _name, int &_minute, bool &_shootout, Scored &_scored, bool &_penality); ~Goal(){}; friend ostream & operator<<(ostream &os, const Goal &_goal); }; --------------- Stadium --------------- class Stadium { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & name; ar & city; ar & spectators; } string name; string city; int spectators; public: Stadium(){}; Stadium(const string _name, const string _city, int &_spectators); friend ostream & operator<<(ostream &os, Stadium &_stadium); }; ---------------- and Match ---------------- class Match { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & home; ar & visitors; ar & kickoff; /*lGoals::const_iterator pos; for (pos = goals.begin(); pos != goals.end(); pos++) { ar & pos; }*/ ar & BOOST_SERIALIZATION_NVP(goals); ar & stadium; } string home; string visitors; time_t kickoff; typedef list<Goal> lGoals; lGoals goals; Stadium stadium; public: Match(){}; Match(const string _home, const string _visitors, time_t &_kickoff, Goal _goals[100], Stadium &_stadium); Match& operator = (Match &_match); friend ostream & operator<<(ostream &out, Match &_match); }; ---------------- Now I have to serialise this classes in an xml structure. Therefor I've created the file xml.h/.cpp. I only post the xml.cpp here: ---------------- #include <iomanip> #include <iostream> #include <fstream> #include <string> #include "goal.h" #include "stadium.h" #include "match.h" #include <boost/serialization/base_object.hpp> #include <boost/serialization/utility.hpp> #include <boost/serialization/list.hpp> #include <boost/serialization/is_abstract.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/archive/xml_oarchive.hpp> //Tracing and Logging module //#include "Logger.h" //#include "Tracer.h" using namespace std; /** * \brief Fuction to Deserialise a xml-file to an object structure * \param const Match &m, const string &filename * \return void */ void save_xml(const Match &m, const string &filename){ // make an archive ofstream ofs(filename.c_str()); boost::archive::xml_oarchive oa(ofs); //fill data to archive oa <<BOOST_SERIALIZATION_NVP(m); ofs.flush(); } /** * \brief Fuction to Serialise an object structure to a xml-file * \param Match &m, const string &filename * \return void */ void restore_xml(Match &m, const string &filename) { // open the archive ifstream ifs(filename.c_str()); boost::archive::xml_iarchive ia(ifs); //restore the data from the archive ia >>BOOST_SERIALIZATION_NVP(m); } ---------------- So thats it and I hope its not all wrong I tried. I hope you can help me finish that project. Thanks!

On Jun 23, 2008, at 7:02 AM, Daniel Kunz wrote:
Hi,
I'm very new at Boost and want to develop a project for school with the Boost:Serialization package.
I've already read the documantation and serveral other sources.
I'm not very good in programming C++, so please excuse that my code looks not perfect.
I hope it's ok that I've posted some sourcecode. I've tried to keep it short...
*My problem:*
I have to create a program to serialise and deserialse a xml-file to a binary-stream. I think to binary stream is already done but the xml-serialisation is not ready and I don't know whats wrong.
[snip]
So thats it and I hope its not all wrong I tried. I hope you can help me finish that project. Thanks!
You never mentioned what you have problems with. What is actually your problem? Matthias

Matthias Troyer schrieb:
On Jun 23, 2008, at 7:02 AM, Daniel Kunz wrote:
Hi,
I'm very new at Boost and want to develop a project for school with the Boost:Serialization package.
I've already read the documantation and serveral other sources.
I'm not very good in programming C++, so please excuse that my code looks not perfect.
I hope it's ok that I've posted some sourcecode. I've tried to keep it short...
*My problem:*
I have to create a program to serialise and deserialse a xml-file to a binary-stream. I think to binary stream is already done but the xml-serialisation is not ready and I don't know whats wrong.
[snip]
So thats it and I hope its not all wrong I tried. I hope you can help me finish that project. Thanks!
You never mentioned what you have problems with. What is actually your problem?
Matthias
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
I wanted to know if the structure of my code and the using of the Serialization functions. Because there are very long copmpile errors. Should I post this errors? Greets Daniel

On Jun 23, 2008, at 9:49 AM, Daniel Kunz wrote:
I wanted to know if the structure of my code and the using of the Serialization functions.
Because there are very long copmpile errors. Should I post this errors?
I would suggest that you start with a very simple problem, just one simple class with a few simple members, and take it from there. Matthias

Matthias Troyer schrieb:
On Jun 23, 2008, at 9:49 AM, Daniel Kunz wrote:
I wanted to know if the structure of my code and the using of the Serialization functions.
Because there are very long copmpile errors. Should I post this errors?
I would suggest that you start with a very simple problem, just one simple class with a few simple members, and take it from there.
Matthias
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Thats not really working for me because the problem requires that complicated class structure and I doesn't have the time to do lots of simle exercises befor it. I purpose that my class Match and the c++-List are responsible for my errors. Can please somebody check this class and /or my function for an error? Daniel

On 24 Jun 2008, at 10:48, Daniel Kunz wrote:
Matthias Troyer schrieb:
On Jun 23, 2008, at 9:49 AM, Daniel Kunz wrote:
I wanted to know if the structure of my code and the using of the Serialization functions.
Because there are very long copmpile errors. Should I post this errors?
I would suggest that you start with a very simple problem, just one simple class with a few simple members, and take it from there.
Matthias
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Thats not really working for me because the problem requires that complicated class structure and I doesn't have the time to do lots of simle exercises befor it.
My suggestion is that you should try it with a very simple subproblem of your problem.
I purpose that my class Match and the c++-List are responsible for my errors.
You never even mentioned what your error was.
Can please somebody check this class and /or my function for an error?
Do you really believe that Boost developers are there to help you with your problem for school if you don't even have time to look at a simplified version of your own problem first? Matthias

Daniel Kunz wrote:
*My problem:*
I have to create a program to serialise and deserialse a xml-file to a binary-stream. I think to binary stream is already done but the xml-serialisation is not ready and I don't know whats wrong.
[...]
template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & name; ar & minute; ar & shootout; ar & scored; ar & penality; }
[...] To serialize to xml, you need wrap all datas in nvp (Name-Value Pairs): void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(name); ar & BOOST_SERIALIZATION_NVP(minute); ar & BOOST_SERIALIZATION_NVP(shootout); ar & BOOST_SERIALIZATION_NVP(scored); ar & BOOST_SERIALIZATION_NVP(penality); } or you will get lots of compile errors, as you said in another post.
participants (3)
-
Daniel Kunz
-
gchen
-
Matthias Troyer