我正在尝试使用Cereal1.1.2序列化和反序列化多态类(具有虚拟继承)。我得到一个‘访问冲突-没有RTTI数据!’当我在反序列化它之后尝试将它向下转换为派生类时,会发生异常。当我使用普通继承而不是虚拟继承时,它工作得很好。我已经在Visual Studio2013社区版的项目设置中启用了RTTI (/GR)。下面是我的代码:
class Boogie
{
friend class cereal::access;
virtual void virtualFunction() {}
int boogieInt = 3;
template<class Archive>
void serialize(Archive & archive)
{
archive(boogieInt);
}
};
class Booga : virtual public Boogie
{
friend class cereal::access;
public:
void virtualFunction() {}
int boogaInt = 2;
template<class Archive>
void serialize(Archive & archive)
{
archive(cereal::virtual_base_class<Boogie>(this), boogaInt);
}
};
CEREAL_REGISTER_TYPE(Booga);
int _tmain(int argc, _TCHAR* argv[])
{
try
{
{
std::shared_ptr<Boogie> boogie = std::make_shared<Booga>();
std::ofstream ofs("Booga.txt");
cereal::BinaryOutputArchive archive(ofs);
archive(boogie);
ofs.close();
}
std::shared_ptr<Boogie> deBoogie;
std::ifstream ifs("Booga.txt");
cereal::BinaryInputArchive iarchive(ifs);
iarchive(deBoogie);
std::shared_ptr<Booga> outBooga = std::dynamic_pointer_cast<Booga>(deBoogie);
std::cout << outBooga->boogaInt << std::endl;
std::cin.get();
}
catch (std::exception e)
{
std::cout << "EXCEPTION" << std::endl;
std::cout << e.what() << std::endl;
}
return 0;
}发布于 2015-09-25 12:50:53
你的问题是你正在保存和加载不同的类型-你的加载代码应该镜像你的保存代码。
还请注意,您不需要在这里使用虚拟继承,因为您只从单个父类派生-有关这方面的更多信息,请参阅here )。此外,请参阅我对您的帖子的评论,了解如何以RAII的方式正确使用归档文件。
您的输出可以指定为:
std::shared_ptr<Boogie> data = std::make_shared<Booga>();
archive( data ); // data is actually a Booga but we hold it in a Boogie ptr注意,我赋值给了一个Boogie对象,即使我分配了一个Booga指针--这基本上就是多态性的全部,除非你需要这样做,否则不要使用多态性。
现在,当我们执行加载时,我们加载到我们序列化的相同类型中:
std::shared_ptr<Boogie> data;
archive( data ); // data is actually a Booga object because of polymorphism只需确保您实际传递给存档的变量的类型是相同的,而不管它们实际上是由于多态而导致的。
https://stackoverflow.com/questions/32246562
复制相似问题