简体   繁体   English

序列化包含指针向量的对象向量

[英]Serializing Vector of Objects which contains Vectors of Pointers

I have 3 classes ("Leader", "Researchers", "Workers") which all derive from a base-class "Team". 我有3个班级(“领导者”,“研究人员”,“工人”),它们都来自基本班级“团队”。

I have a class "Project" which contains a vector of pointers to different Teams. 我有一个“项目”类,其中包含指向不同团队的指针向量。

I use all of the following headers, in this order, in all of my class declarations: 我在所有类声明中都以此顺序使用以下所有标头:

#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/split_member.hpp>

To (de)serialize the Team object I use: 要对团队对象进行反序列化:

private:
  friend class boost::serialization::access ;

  template <typename Archive>
  void serialize(Archive& ar, const unsigned int /*version*/)
  {
      ar & teamname ;
  }

To (de)serialize the Leader, Researchers, Workers objects I use: 为了(反)序列化领导者,研究人员,工人对象,我使用了:

typedef Team _super;

friend class boost::serialization::access ;

template <typename Archive>
void serialize(Archive& ar, const unsigned int /*version*/)
{
    ar & boost::serialization::base_object<_super>(*this) ;
    ar & contactTelephoneNumber ;
}

The Project holds a std::vector of pointers to different teams and a string using: 该项目包含指向不同团队的std :: vector指针和使用以下内容的字符串:

std::vector<Team *> teams ;
std::string note ;

I use the following code in the Project class for serialization: 我在Project类中使用以下代码进行序列化:

private:
  friend class boost::serialization::access ;

  template <typename Archive>
  void serialize(Archive& ar, const unsigned int /*version*/)
  {
      //ar & BOOST_SERIALIZATION_NVP(teams) ; //ERROR OCCURS HERE
      ar & teams;
      ar & note ;
  }

And to serialize the vector of Project objects in the main I use: 为了序列化Project对象的向量,我主要使用:

{
    std::ostringstream archiveStream ;
    boost::archive::text_oarchive archive(archiveStream) ;
    archive << BOOST_SERIALIZATION_NVP(projects) ;

    //Get serialized info as string
    archivedProjects = archiveStream.str() ;
}

This all compiles fine. 所有这些都可以编译。 The issue is at Run-Time. 问题是在运行时。 When the above section of the code is reached, I get the following error: 达到上述代码部分时,出现以下错误:

terminate called after throwing an instance of 'boost::archive::archive_exception' 
what(): 
    unregistered class - derevided class not registered or exported"

The program goes as far as: 该程序包括:

ar & teams;

In the Questionnaire class's serialization attempt. 在调查表类的序列化尝试中。

As in nm's link: you need to register the classes with Boost so that it knows what classes are what when serialising. 就像在nm的链接中一样:您需要向Boost注册类,以便它知道序列化时是什么类。

You need to add the following line for each class where "Project" serializes: 您需要为“项目”进行序列化的每个类添加以下行:

ar.template register_type<ClassName>() ; //ClassName = Team etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM