序列化包含指针向量的对象向量 [英] Serializing Vector of Objects which contains Vectors of Pointers
问题描述
我有3个班级(领导者",研究人员",工人"),这些班级均来自基本班级团队".
I have 3 classes ("Leader", "Researchers", "Workers") which all derive from a base-class "Team".
我有一个项目"类,其中包含指向不同团队的指针的向量.
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>
要(反)序列化我使用的Team对象:
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 ;
}
该项目包含指向不同团队的指针的std :: vector和使用以下内容的字符串:
The Project holds a std::vector of pointers to different teams and a string using:
std::vector<Team *> teams ;
std::string note ;
我在Project类中使用以下代码进行序列化:
I use the following code in the Project class for serialization:
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 ;
}
要序列化Project对象的向量,我主要使用:
And to serialize the vector of Project objects in the main I use:
{
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.
推荐答案
就像在n.m.的链接中一样:您需要向Boost注册类,以便它知道序列化时是什么类.
As in n.m.'s link: you need to register the classes with Boost so that it knows what classes are what when serialising.
您需要为序列化项目"的每个类添加以下行:
You need to add the following line for each class where "Project" serializes:
ar.template register_type<ClassName>() ; //ClassName = Team etc
这篇关于序列化包含指针向量的对象向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!