Re: [Boost-users] [serialization] problem with derived classes
Hello,
I'm having an issue with serializing derived classes from a base class pointer. I think I've narrowed it down to the simplest possible code. I get an "unregistered class" exception at runtime even though I've registered the derived class with BOOST_CLASS_EXPORT. Here's the code:
[ snip ]
-----------------------------------------------
X.hpp:
#pragma once
#include
#include "A.hpp" class X : public A { friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int) { ar & boost::serialization::base_object<A>(*this); } };
------------------------------------------------
X.cpp:
#include "X.hpp"
#include
BOOST_CLASS_EXPORT(X)
-----------------------------------------------
As you can see, I don't have BOOST_CLASS_EXPORT in the X.hpp header. This is the way I originally had it but then I got multiple definition link errors when more than one class derived from A. The suggestion in the serialization docs was to move the macro to a .cpp file. [ snip ]
Hello Avery, I suspect You need to BOOST_CLASS_EXPORT(A) as well, since You do try to serialize an A in X's serialize() method. This is was I ended up doing when serializing a hierarchy of derived classes and a base class. I'm not sure how to do that in Your case, since I had all classes and all BOOST_CLASS_EXPORT macros in a single header file. HTH, Johan Franzén
On Wed, 2007-27-06 at 21:10 +0200, Johan Franzén wrote:
Hello Avery,
I suspect You need to BOOST_CLASS_EXPORT(A) as well, since You do try to serialize an A in X's serialize() method. This is was I ended up doing when serializing a hierarchy of derived classes and a base class. I'm not sure how to do that in Your case, since I had all classes and all BOOST_CLASS_EXPORT macros in a single header file.
HTH, Johan Franzén
I think Johan is right. I've found that the safest way to do polymorphic serialization is export ALL your classes, always in cpp files. Templates are another story. :) Sohail
Thanks for your replies. It turns out that you don't need to export the base
class. I've done some experimenting and this is what I've come up with.
Hopefully, this will help someone else.
1.) You can export from a header file, but if you do you can't include that
header more than one time in your program or you get multiple definition
errors. This goes for one export per header as well as all exports in one
header.
2.) Exporting from a source file doesn't work as I posted before. You *have*
to include the iarchive and oarchive headers in your cpp file you're
exporting even if they aren't needed there. Missing one or the other or both
leads to exceptions or plain old crashes.
Unless I'm missing something, the whole export system seems incredibly
fragile.
Avery
On 6/27/07, Sohail Somani
On Wed, 2007-27-06 at 21:10 +0200, Johan Franzén wrote:
Hello Avery,
I suspect You need to BOOST_CLASS_EXPORT(A) as well, since You do try to serialize an A in X's serialize() method. This is was I ended up doing when serializing a hierarchy of derived classes and a base class. I'm not sure how to do that in Your case, since I had all classes and all BOOST_CLASS_EXPORT macros in a single header file.
HTH, Johan Franzén
I think Johan is right. I've found that the safest way to do polymorphic serialization is export ALL your classes, always in cpp files.
Templates are another story.
:)
Sohail
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
"Avery Fay"
-----Original Message----- Subject: Re: [Boost-users] [serialization] problem with derived classes *** it IS fragile. The latest version by D. Abrahams is much inproved and will be part of bost 1.35. -----End Original Message----- Hi Robert, What are the improvements?
participants (4)
-
Avery Fay
-
Johan Franzén
-
Robert Ramey
-
Sohail Somani