首页 > 解决方案 > 以递归方式从文件中提升 C++ 序列化和反序列化对象

问题描述

我有一个用户定义的“集合”集合。我有一个包含一组对象的对象。我正在尝试序列化和反序列化这组对象。

 class Support_Index:   used to create Element objects and insert into an OSet.
 class Element:         Collectable  - Object that has data ( just a const char* )
 class OSet:            Collectable  - collection type that holds Element objects
 class OFile:           used for the shift operator defines 

在示例代码中,它首先将 2 个 Element 对象插入到 OSet 集合中。

然后调用将 OSet ( lo_terms ) 保存到 tmp 文件的函数。拯救似乎成功了。

然后调用一个函数从文件中恢复内容,该文件刚刚在保存中使用。

问题在于加载功能。它具有递归逻辑,但问题是在执行此函数时,它经过一次(对象计数为 1)然后返回(递归)并继续计数为 2(这是我所期望的)第一次)。当它尝试加载 2 个对象时,我得到了 SEH 异常。

程序.cpp

bool ArchiveOSetElementsToFile(bool verbose)
{   

  const char *filename = "C:\\temp\\ArchiveOSetElementsToFile.tmp"; 
  OFile file(filename);

  IndexSupport.add_lterm("a");
  IndexSupport.add_lterm("b");    

  if (IndexSupport.lterm_entries() > 0) {         
     IndexSupport.save_lterms(file);
    }       

  IndexSupport.restore_lterms(file);        

  return passed;
}

OSet 负载

template<class Archive>
void load(Archive & ar, const unsigned int version)
  {
      std::cout << "load ASet" << std::endl;
      ar & boost::serialization::base_object<mycollect>(*this);
      boost::serialization::collection_size_type count;
      ar & count;
      std::cout << "count: " << count << std::endl;
      unsigned int i = 0;
      while (i < count)
      {
        std::cout << "load ASet Recursive Call " << std::endl;
        mycollect *thismycollect;
        ar & thismycollect;
        insert(thismycollect);
        i++;
      }
      std::cout << "loaded: " << _set.entries() << std::endl;
  }

在执行 ar & thismycollect; 是它获得 SEH 异常的地方。

要知道的事情。数据来自不在内存中的文件。
这是我收到的编译器输出

output_anatom:

  load  OSet <-- derives off of myCollect class
  count: 1
  load OSet Recursive Call
  load OSet
  count: 2
  load OSet Recursive Call

未处理的异常:System.Runtime.InteropServices.SEHException:外部组件已引发异常。在 _mainCRTStartup()

所以..我无法得到这个网站上的所有代码,所以总结一下。

 Support_Index starts the process 
 Support_Index creates Element objects and places them in an OSet collectable object.

 Support_Index call the boost save which seems to save just fine
 when Support_Index calls load it excepts on the recursive call in the load function.

任何想法或帮助总是受到赞赏。

如果您还需要什么,请告诉我。我确定我遗漏了您可能希望看到的代码。也许我可以把那块给你。

标签: c++boost

解决方案


推荐阅读