【问题标题】:Serialize and Deserialize a Graph with Cycles使用循环序列化和反序列化图形
【发布时间】:2014-12-05 13:46:32
【问题描述】:

我正在尝试使用Boost 库对对象进行序列化(保存)和反序列化(重新加载),以便最大限度地减少开销内存需求,因为我创建了多个对象。我是 Boost 库的新手,但到目前为止我不想修改我正在使用的库。我尝试编写下面的代码,但出现错误 {error C2039: 'serialize' : is not a member of 'DirectedGraphicalModels::CGraph'E:\external\boost\boost_1_54_0\boost\serialization\access.hpp 118}。我正在使用的图形库的标题是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);

}

请告诉我如何继续保存并重新加载图表以进行进一步处理。到目前为止,我已经参考了这篇文章12,但我还没有清楚地理解这些概念。 提前致谢。

【问题讨论】:

    标签: c++ serialization boost


    【解决方案1】:

    您需要实现序列化功能。看到你不想修改库头,添加这样的助手:

    #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.
    
    } }
    

    默认情况下,对象跟踪是对通过指针(反)序列化的所有对象进行的。这可以确保别名指针不会被(反)序列化两次,这意味着您可以很好地(反)序列化循环图。

    【讨论】:

    • 我尝试实现你的代码,我得到这个错误“34 IntelliSense: namespace definition is not allowed main.cpp 207” 可能是什么问题? "
    • 要么将代码发布在错误的位置(在类或函数内?o.O),要么只需要忽略 IntelliSense 并注意编译器错误注意,我编造了 title()sourcetarget 之类的东西。我会再次将它们注释掉以使其更清楚。
    • 我试过了,还是一样的错误,没有编译成功。这是否意味着我必须将它创建为一个类?目前,我直接在 main 中使用它。
    • 命名空间显然不能进入函数或类。只需将其作为命名空间范围(例如全局)即可。
    • 我是否必须指定与节点(即float)和Edges(即double)相对应的数据类型,而不是您在代码中指定的const unsigned?还是没关系?
    猜你喜欢
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    相关资源
    最近更新 更多