简体   繁体   中英

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 . I have a hierarchy of classes and, thus, I use the register_type() .

However, where I came to use binary_iarchive to unserialize my objects, I got

error: cannot allocate an object of abstract type

which comes from the call of register_type() on the input archive.

After googling I found out that the macro BOOST_SERIALIZATION_ASSUME_ABSTRACT(T) must be used for the abstract classes. 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.

error: cannot allocate an object of abstract type

It seems somewhere in your code, you're creating instance(s) of an abstract class. That is why you're getting this error, because creating instance of abstract class is forbidden. You can create instance(s) of only concrete classes.

You need to tell boost that X is an abstract class, by writing:

BOOST_SERIALIZATION_ASSUME_ABSTRACT(X);

Now follow this topic which explains it further:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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