Serialize abstract base class
I try to serialize objects wich are derived of an abstract base class. class Modul() { Modul(); int a; virtual void function() = 0; private: friend class boost::serialization::access; template <typename Archive> void serialize(Archive &ar, const unsigned int version) { ar & boost::serialization::make_nvp("a", a); }; }; BOOST_SERIALIZATION_ASSUME_ABSTRACT(Modul) BOOST_CLASS_TRACKING(Modul, track_never) class ExampleModul : public Modul { ExampleModul(){}; int b; void function(); private: friend class boost::serialization::access; template <typename Archive> void serialize(Archive &ar, const unsigned int version) { ar & boost::serialization::make_nvp("Modul", boost::serialization::base_object<Modul>(*this)); ar & boost::serialization::make_nvp("b", b); } }; For serializing the classes I use a boost::archive::xml_oarchive and register the classes: boost::archive::xml_oarchive archfile; archfile.register_type<Modul>(); archfile.register_type<ExampleModul>(); I can compile serializing and run the code - works. But when it comes to deserializing I get a compile error: ..instantiated from here /usr/include/boost/serialization/access.hpp:132:9: error: cannot allocate an object of abstract type 'Modul' note: because the following virtual functions are pure within 'Modul': virtual void Modul::function() If I do not register <Modul> in the deserializing code, then it compiles but I get runtime error of course: terminate called after throwing an instance of 'boost::archive::archive_exception' I found this example but I dont understand what they are doing and where the classes are registered: http://www.boost.org/doc/libs/1_47_0/libs/serialization/example/demo.cpp How can I serialize my abstract base class?
If I do not register base classe <Modul>, nor in serializing code nor in
deserializing code:
It compliles and serializing runs and creates perfect archive.
But when I try to deserialize the archive I get a runtime error:
terminate called after throwing an instance of
'boost::archive::archive_exception'
what(): stream error
How do I de/serialize: I created a class and use a vector of base class
pointers:
class LogElement
{
public:
LogElement();
virtual ~LogElement(){};
std::vector
Fabian Weiss wrote:
How can I serialize my abstract base class?
you should only need to register the derived classes.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Fabian Weiss wrote:
If I do not register base classe <Modul>, nor in serializing code nor in deserializing code: It compliles and serializing runs and creates perfect archive. But when I try to deserialize the archive I get a runtime error:
Note that the serialization and de-serialization code should be identical. That is, a) if you register on saving you have to do so on loading. b) if you save as a pointer to Module, you have to load as a pointer to Module c) if you save as a pointer to DerivedModule, you have to load as a pointer to DerivedModule. ... etc... Double check this.
terminate called after throwing an instance of 'boost::archive::archive_exception' what(): stream error
How do I de/serialize: I created a class and use a vector of base class pointers:
class LogElement { public: LogElement(); virtual ~LogElement(){};
std::vector
Module; private: friend class boost::serialization::access;
template <typename Archive> void serialize(Archive &ar, const unsigned int version) { ar & boost::serialization::make_nvp("Module", Module); }; };
In Module are pointers to objects of derived classes. Is it a problem?
Am 03.11.2011 06:09, schrieb Robert Ramey:
Fabian Weiss wrote:
How can I serialize my abstract base class?
you should only need to register the derived classes.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Fabian Weiss
-
Robert Ramey