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();


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

  #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;
       int x;
       gps_set::gps_set(){ x = 2;};
       virtual gps_set::~gps_set(){};
       gps_set::gps_set(int i) : x(1) {};
       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

  Andreas Saebjoernsen


  Boost-users mailing list

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at