0

I am trying to serialize (save) and deserialize (reload) an object using Boost library in order to minimize overhead memory requirements since I created several objects. I am new to Boost library but so far I do not wish to modify the libraries I am using. I tried writing the code below but I got an error {error C2039: 'serialize' : is not a member of 'DirectedGraphicalModels::CGraph'E:\external\boost\boost_1_54_0\boost\serialization\access.hpp 118}. The header of the Graph library I am using is here

int main(int argc, char *argv[])
{
CImageGraph *pGraph     = new CGraph(nStates);  
cout << "Building the Graph..." << endl;
pGraph->buildImageGraphN4(fv.rows, fv.cols, pEdgeTrainer != NULL, true);

 // Save data
  {
    const char* fileName = "Graph.txt";
    // Create an output archive
    std::ofstream ofs(fileName);
    boost::archive::text_oarchive ar(ofs);
    // Save only the pointer. This will trigger serialization
    // of the object it points too, i.e., o1.
    ar & pGraph;
  }

  // Restore data
  CImageGraph *pRGraph     = new CGraph(nStates);   
  cout << "Building the Graph for restore..." << endl;
  pRGraph->buildImageGraphN4(fv.rows, fv.cols, pEdgeTrainer != NULL, true);
  pRGraph;
  {
     const char* fileName = "Graph.txt";
    // Create and input archive
    std::ifstream ifs(fileName);
    boost::archive::text_iarchive ar(ifs);
    // Load
    ar & pRGraph;
  }

  // Make sure we read exactly what we saved.
  assert(pRGraph != pRGraph);
  //assert(*pRGraph == pRGraph);

}

Kindly advice me on how I can go ahead and save and reload the Graph for further processing. So far I have referred to this articles 1 and 2 but I have not clearly understood the concepts. Thanks in advance.

Community
  • 1
  • 1
Benson
  • 151
  • 1
  • 2
  • 9

1 Answers1

0

You need to implement the serialization functions. Seeing you don't want to modify the library headers, add helpers like such:

#include <boost/serialization/vector.hpp>

namespace boost { namespace serialization {

      template <typename Ar>
      void serialize(Ar& ar, DirectGraphicalModels::CGraph& graph, const unsigned int version) {
           //// e.g.: 
           // ar & graph.title();
           // ar & graph.nodes(); // will use the default vector adaptation from the header above
           // ar & graph.edges(); // will use the default vector adaptation from the header above
      }

      template <typename Ar>
      void serialize(Ar& ar, DirectGraphicalModels::Node& node, const unsigned int version) {
           //// e.g.: 
           // ar & node.source;
           // ar & node.target;
      }

      // etc.

} }

By default object tracking is done on all objects (de)serialized through pointers. This makes sure aliased pointers do not get (de)serialized twice, which means you can (de)serialize cyclic graphs fine.

See

sehe
  • 374,641
  • 47
  • 450
  • 633
  • I tried implementing you code and I get this error "34 IntelliSense: namespace definition is not allowed main.cpp 207" What could be the problem?? " – Benson Dec 05 '14 at 16:36
  • Either you posted the code in the wrong location (inside a class or function? o.O) or you just need to ignore IntelliSense and watch for _compiler errors_ instead. **Note** that I made up the things like `title()`, `source`, `target`. I'll comment them out again to make it clearer. – sehe Dec 06 '14 at 01:43
  • I have tried and still have the same error no compilation success. Does it mean I have to create it as a class? Currently, I am using it directly inside main. – Benson Dec 06 '14 at 14:32
  • Namespaces obviously cannot go inside either functions or classes. Just put it as namespace scope (e.g. global). – sehe Dec 08 '14 at 08:30
  • Do I have to specify the data type corresponding to nodes (i.e. float) and Edges (i.e. double) as opposed to const unsigned you specified in the code? Or it does not matter? – Benson Jan 21 '15 at 14:31
  • It does matter. And no `const unsigned` cannot change: it's part of the required [`Serializable` concept](http://www.boost.org/doc/libs/1_57_0/libs/serialization/doc/serialization.html#free). If you need more specific hints, you need to show more specific context/code. Consider asking a new question with precisely what you are stuck with. – sehe Jan 21 '15 at 14:38