Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-01-14 00:15:24


This has nothing to do with seriaization - its a quirk of C++ syntax. Replace

     const gps_set s1();

with

     const gps_set s1;

and your program will compile and execute as you expect.

The problem is that C++ interprets the formor as declaration of a function named s1 which returns a const gps_set. Trying
to serialize that traps on the notorious "const check" which is just a total coincidence.

The later will create a const gps_set object on the stack which can be serialized with no problem.

To really understand this, you'll have to dig into a little used section of one of your C++ references.

Good Luck,

Robert Ramey

  "Andreas Sæbjørnsen" <andreas.saebjoernsen_at_[hidden]> wrote in message news:954f1fae0601131820sfd56f77pb31a9d114f8a343e_at_mail.gmail.com...
  Hi,
  I am currently experimenting with writing a compiler tool which generates the code necessary for using boost::serialization. While working on this I found that I am unable to serialize a variable declaration which is a reference to a class constructed with the default constructor '()'( ex. class foo var(); ). I have tried to find an explanation for this in the examples, documentation and through searching the web without success. Using pointers it works fine with the
  default constructor and I do not see why it should be any different for references (as references in essence are pointers). Is it possible to serialize a class constructed using the default constructor using boost::serialization, and if so how?

  Example:
  #include <fstream>
                                                                                                                                                              
  // include headers that implement a archive in simple text format
  #include <boost/archive/text_oarchive.hpp>
  #include <boost/archive/text_iarchive.hpp>
                                                                                                                                                                
  class gps_set
  {
    friend class boost::serialization::access;
                                                                                                                                                                
    public:
       int x;
       gps_set::gps_set(){ x = 2;};
       virtual gps_set::~gps_set(){};
       gps_set::gps_set(int i) : x(1) {};
                                                                                                                                                                
    private:
       template<typename Archive>
       void serialize(Archive& ar, const unsigned int version){
                 ar& x;
       }// End method: X::serialization
  };
  int main()
  {
      // save the schedule
       std::ofstream ofs("filename");
                                                                                                                                                              
      // create class instance
      //Compile time error if s1() is used. No error if s1(1) is used.
       const gps_set s1();
      // save data to archive
      {
        boost::archive::text_oarchive oa(ofs);
          // write class instance to archive
          oa << s1;
          // archive and stream closed when destructors are called
      }
  };

  Thanks,
  Andreas Saebjoernsen

------------------------------------------------------------------------------

  _______________________________________________
  Boost-users mailing list
  Boost-users_at_[hidden]
  http://lists.boost.org/mailman/listinfo.cgi/boost-users



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net