简体   繁体   English

用抽象 class 提升二进制序列化问题

[英]Boost binary serialization problem with abstract class

Ciao folks,乔人,

I'm quiet desperate not to find the source of the problem.我很安静,不希望找到问题的根源。 I'm using Boost:serialization (version 1.46.1) and everything was working fine with binary_oarchive .我正在使用 Boost:serialization (版本 1.46.1),并且使用binary_oarchive一切正常。 I have a hierarchy of classes and, thus, I use the register_type() .我有一个类层次结构,因此,我使用register_type()

However, where I came to use binary_iarchive to unserialize my objects, I got然而,当我开始使用binary_iarchive来反序列化我的对象时,我得到了

error: cannot allocate an object of abstract type错误:无法分配抽象类型的 object

which comes from the call of register_type() on the input archive.它来自对输入存档的register_type()调用。

After googling I found out that the macro BOOST_SERIALIZATION_ASSUME_ABSTRACT(T) must be used for the abstract classes.谷歌搜索后,我发现宏BOOST_SERIALIZATION_ASSUME_ABSTRACT(T)必须用于抽象类。 The problem is that it simply doesn't work:).问题是它根本不起作用:)。 Can anyone help me out?谁能帮我吗?

More about my code:更多关于我的代码:

virtual void AbstractClass::initBinarySerialization(binary_iarchive& ia)
        {
            ia.register_type<AbstractClass>();
        }

virtual void SubClass::initBinarySerialization(binary_iarchive& ia)
        {
            AbstractClass::initBinarySerialization(ia);
            ia.register_type<SubClass>();
        }

I call initBinarySerialization before reading in the archive.在阅读存档之前,我调用initBinarySerialization

error: cannot allocate an object of abstract type错误:无法分配抽象类型的 object

It seems somewhere in your code, you're creating instance(s) of an abstract class.似乎在您的代码中的某处,您正在创建抽象class 的实例。 That is why you're getting this error, because creating instance of abstract class is forbidden.这就是您收到此错误的原因,因为禁止创建抽象class 的实例。 You can create instance(s) of only concrete classes.您只能创建具体类的实例。

You need to tell boost that X is an abstract class, by writing:你需要告诉 boost X 是一个抽象的 class,方法是:

BOOST_SERIALIZATION_ASSUME_ABSTRACT(X);

Now follow this topic which explains it further:现在按照这个主题进一步解释它:

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

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