Tony Camuso
Tony Camuso

Reputation: 23

Qt boost serialize compile errors on simple implementation

Platform specifics: Fedora 21 g++ 4.9.2-6

Here's the error ....

$ g++ -c kabi-serial.cpp -lboost_serialization
kabi-serial.cpp: In function ‘void boost::serialization::kb_write_list()’:
kabi-serial.cpp:41:13: error: expected ‘;’ before ‘oa’
   serialize oa(ofs);

kabi-serial.cpp:41:20: error: statement cannot resolve address of overloaded function
   serialize oa(ofs);
                ^
kabi-serial.cpp:42:3: error: ‘oa’ was not declared in this scope
   oa << ql;

And here's the kabi-serial.cpp source ...

#include <boost/serialization/vector.hpp>
#include <vector>

namespace boost {
namespace serialization {

class Cqnodelist
{
public:
     friend class boost::serialization::access;
     Cqnodelist(){}
     std::vector<int>qnodelist;

     template<class Archive>
     void serialize(Archive & ar, const unsigned int version)
     {
         ar & qnodelist;
     }
 };

 void kb_write_list ()
 {
     Cqnodelist ql;

     ofstream ofs("kabi-list.dat");
     {
         serialize oa(ofs);
         oa << ql;
     }
 }
 }
 }

I based the source code on the simplest of the serialization examples, except using the serialization of the stl vector object.

I'm sure it's simple, but what am I missing?

Upvotes: 0

Views: 188

Answers (1)

rutsky
rutsky

Reputation: 4110

To use ofstream you need to include <fstream>.

ofstream is in the namespace std: std::ofstream.

You don't need to put your code inside boost::serialization namespace (except case when you implement external to class serialize() function).

Adding serialize method implements way of loading and saving object attributes. To actually store or load data you need to select storage — archive in Boost.Serialization terminology.

#include <boost/serialization/vector.hpp>

// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

#include <vector>
#include <fstream> // required for std::ofstream

class Cqnodelist
{
public:
    friend class boost::serialization::access;
    Cqnodelist(){}
    std::vector<int>qnodelist;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & qnodelist;
    }
};

void kb_write_list ()
{
    Cqnodelist ql;

    // fill object with data
    ql.qnodelist.push_back(1);
    ql.qnodelist.push_back(2);
    ql.qnodelist.push_back(3);

    // See http://www.boost.org/doc/libs/1_57_0/libs/serialization/doc/tutorial.html
    // for reference

    // save data to archive
    {
       // open file that will contain serialized data
       std::ofstream ofs("kabi-list.dat");

       // create archive on top of opened file
       boost::archive::text_oarchive oa(ofs);
       // write class instance to archive
       oa << ql;
    }

    // to load object later open file, create input archive on top of opened
    // file and load object state
    {
       Cqnodelist new_ql;

       // create and open an archive for input
       std::ifstream ifs("kabi-list.dat");
       boost::archive::text_iarchive ia(ifs);
       // read class state from archive
       ia >> new_ql;
    }
}

int main()
{
    kb_write_list();
}

Upvotes: 1

Related Questions