Serialization of a std::vector< boost::shared_ptr< A > > throws unregistered_class exception

Given the following classes: class Building { ... }; class House : public Building { ... }; class Hospital : public Building { ... }; With appropriate serialization method templates such as: class House: public Building { friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned int version ) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Building ); ar & BOOST_SERIALIZATION_NVP( mNumBedrooms ); ar & BOOST_SERIALIZATION_NVP( mNumBathrooms ); } ... I have the following registrations in main.cpp where the archiving/ restoring is performed: BOOST_SERIALIZATION_SHARED_PTR( Building ) BOOST_SERIALIZATION_SHARED_PTR( House ) BOOST_SERIALIZATION_SHARED_PTR( Hospital ) In main.ccp, I would like to serialize a std::vector< boost::shared_ptr< Building > > object. Like this: std::vector< boost::shared_ptr< Building > > buildings; ... std::ofstream ofs( "archive.txt" ); boost::archive::text_oarchive oa( ofs ); oa & BOOST_SERIALIZATION_NVP( buildings ); This compiles correctly but fails at runtime with a "unregistered_class" exception. The individual classes (Building, House, Hospital) can be serialized and restored fine individually. That is true for objects, pointers and shared_ptr. A vector of plain pointers to any of those types also works fine. But a vector of shared_ptr to any of those types fails at runtime.. What kind of registration must be done to achieve this?

Depending on which version of boost you're using. You may have to include a special macro - look into serialization/shared_ptr.hpp at the end. Robert Ramey Benoit Gagnon wrote:
Given the following classes:
class Building { ... }; class House : public Building { ... }; class Hospital : public Building { ... };
With appropriate serialization method templates such as:
class House: public Building { friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned int version ) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Building ); ar & BOOST_SERIALIZATION_NVP( mNumBedrooms ); ar & BOOST_SERIALIZATION_NVP( mNumBathrooms ); } ...
I have the following registrations in main.cpp where the archiving/ restoring is performed:
BOOST_SERIALIZATION_SHARED_PTR( Building ) BOOST_SERIALIZATION_SHARED_PTR( House ) BOOST_SERIALIZATION_SHARED_PTR( Hospital )
In main.ccp, I would like to serialize a std::vector< boost::shared_ptr< Building > > object. Like this:
std::vector< boost::shared_ptr< Building > > buildings; ... std::ofstream ofs( "archive.txt" ); boost::archive::text_oarchive oa( ofs ); oa & BOOST_SERIALIZATION_NVP( buildings );
This compiles correctly but fails at runtime with a "unregistered_class" exception. The individual classes (Building, House, Hospital) can be serialized and restored fine individually. That is true for objects, pointers and shared_ptr. A vector of plain pointers to any of those types also works fine. But a vector of shared_ptr to any of those types fails at runtime..
What kind of registration must be done to achieve this?

On Mar 19, 9:58 am, "Robert Ramey"
Depending on which version of boost you're using. You may have to include a special macro - look into serialization/shared_ptr.hpp at the end.
Robert Ramey
I'm using Boost 1.34.1. The only macro I see in serialization/shared_ptr.hpp is BOOST_SERIALIZATION_SHARED_PTR. Am I missing something?
Benoit Gagnon wrote:
Given the following classes:
class Building { ... }; class House : public Building { ... }; class Hospital : public Building { ... };
With appropriate serialization method templates such as:
class House: public Building { friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned int version ) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Building ); ar & BOOST_SERIALIZATION_NVP( mNumBedrooms ); ar & BOOST_SERIALIZATION_NVP( mNumBathrooms ); } ...
I have the following registrations in main.cpp where the archiving/ restoring is performed:
BOOST_SERIALIZATION_SHARED_PTR( Building ) BOOST_SERIALIZATION_SHARED_PTR( House ) BOOST_SERIALIZATION_SHARED_PTR( Hospital )
In main.ccp, I would like to serialize a std::vector< boost::shared_ptr< Building > > object. Like this:
std::vector< boost::shared_ptr< Building > > buildings; ... std::ofstream ofs( "archive.txt" ); boost::archive::text_oarchive oa( ofs ); oa & BOOST_SERIALIZATION_NVP( buildings );
This compiles correctly but fails at runtime with a "unregistered_class" exception. The individual classes (Building, House, Hospital) can be serialized and restored fine individually. That is true for objects, pointers and shared_ptr. A vector of plain pointers to any of those types also works fine. But a vector of shared_ptr to any of those types fails at runtime..
What kind of registration must be done to achieve this?
_______________________________________________ Boost-users mailing list Boost-us...@lists.boost.orghttp://lists.boost.org/mailman/listinfo.cgi/boost-users

On Mar 19, 10:26 am, Benoit Gagnon
On Mar 19, 9:58 am, "Robert Ramey"
wrote: Depending on which version of boost you're using. You may have to include a special macro - look into serialization/shared_ptr.hpp at the end.
Robert Ramey
I'm using Boost 1.34.1. The only macro I see in serialization/shared_ptr.hpp is BOOST_SERIALIZATION_SHARED_PTR.
Am I missing something?
I still haven't figured this out, if anyone has clues... Should I make a wrapper class around the std::vector and implement serialization for it?
Benoit Gagnon wrote:
Given the following classes:
class Building { ... }; class House : public Building { ... }; class Hospital : public Building { ... };
With appropriate serialization method templates such as:
class House: public Building { friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned int version ) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Building ); ar & BOOST_SERIALIZATION_NVP( mNumBedrooms ); ar & BOOST_SERIALIZATION_NVP( mNumBathrooms ); } ...
I have the following registrations in main.cpp where the archiving/ restoring is performed:
BOOST_SERIALIZATION_SHARED_PTR( Building ) BOOST_SERIALIZATION_SHARED_PTR( House ) BOOST_SERIALIZATION_SHARED_PTR( Hospital )
In main.ccp, I would like to serialize a std::vector< boost::shared_ptr< Building > > object. Like this:
std::vector< boost::shared_ptr< Building > > buildings; ... std::ofstream ofs( "archive.txt" ); boost::archive::text_oarchive oa( ofs ); oa & BOOST_SERIALIZATION_NVP( buildings );
This compiles correctly but fails at runtime with a "unregistered_class" exception. The individual classes (Building, House, Hospital) can be serialized and restored fine individually. That is true for objects, pointers and shared_ptr. A vector of plain pointers to any of those types also works fine. But a vector of shared_ptr to any of those types fails at runtime..
What kind of registration must be done to achieve this?
_______________________________________________ Boost-users mailing list Boost-us...@lists.boost.orghttp://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-us...@lists.boost.orghttp://lists.boost.org/mailman/listinfo.cgi/boost-users

Benoit Gagnon wrote:
On Mar 19, 10:26 am, Benoit Gagnon
wrote: On Mar 19, 9:58 am, "Robert Ramey"
wrote: Depending on which version of boost you're using. You may have to include a special macro - look into serialization/shared_ptr.hpp at the end. Robert Ramey I'm using Boost 1.34.1. The only macro I see in serialization/shared_ptr.hpp is BOOST_SERIALIZATION_SHARED_PTR.
Am I missing something?
I still haven't figured this out, if anyone has clues... Should I make a wrapper class around the std::vector and implement serialization for it?
Do you have BOOST_CLASS_EXPORT for each class that you are serializing
by base class pointer? For example, Hospital. You should have something
like:
#include

On Mar 20, 2:13 pm, Sohail Somani
Benoit Gagnon wrote:
On Mar 19, 10:26 am, Benoit Gagnon
wrote: On Mar 19, 9:58 am, "Robert Ramey"
wrote: Depending on which version of boost you're using. You may have to include a special macro - look into serialization/shared_ptr.hpp at the end. Robert Ramey I'm using Boost 1.34.1. The only macro I see in serialization/shared_ptr.hpp is BOOST_SERIALIZATION_SHARED_PTR.
Am I missing something?
I still haven't figured this out, if anyone has clues... Should I make a wrapper class around the std::vector and implement serialization for it?
Do you have BOOST_CLASS_EXPORT for each class that you are serializing by base class pointer? For example, Hospital. You should have something like:
#include
class Hospital ...;
BOOST_CLASS_EXPORT(Hospital)
That did it, thank you!
-- Sohail Somanihttp://uint32t.blogspot.com
_______________________________________________ Boost-users mailing list Boost-us...@lists.boost.orghttp://lists.boost.org/mailman/listinfo.cgi/boost-users

Benoit Gagnon wrote:
On Mar 20, 2:13 pm, Sohail Somani
wrote: Am I missing something? I still haven't figured this out, if anyone has clues... Should I make a wrapper class around the std::vector and implement serialization for it? Do you have BOOST_CLASS_EXPORT for each class that you are serializing by base class pointer? For example, Hospital. You should have something
Benoit Gagnon wrote: like:
#include
class Hospital ...;
BOOST_CLASS_EXPORT(Hospital)
That did it, thank you!
Great! IMHO, with Boost Serialization, more than others, you really do need to read the *whole* documentation before you write code. It will pay dividends. I'm pretty sure this exact case is covered somewhere in the docs. -- Sohail Somani http://uint32t.blogspot.com

Consider including the following BOOST_CLASS_EXPORT(House); BOOST_CLASS_EXPORT(Hospital); Robert Ramey Benoit Gagnon wrote:
On Mar 19, 10:26 am, Benoit Gagnon
wrote: On Mar 19, 9:58 am, "Robert Ramey"
wrote: Depending on which version of boost you're using. You may have to include a special macro - look into serialization/shared_ptr.hpp at the end.
Robert Ramey
I'm using Boost 1.34.1. The only macro I see in serialization/shared_ptr.hpp is BOOST_SERIALIZATION_SHARED_PTR.
Am I missing something?
I still haven't figured this out, if anyone has clues... Should I make a wrapper class around the std::vector and implement serialization for it?
Benoit Gagnon wrote:
Given the following classes:
class Building { ... }; class House : public Building { ... }; class Hospital : public Building { ... };
With appropriate serialization method templates such as:
class House: public Building { friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned int version ) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Building ); ar & BOOST_SERIALIZATION_NVP( mNumBedrooms ); ar & BOOST_SERIALIZATION_NVP( mNumBathrooms ); } ...
I have the following registrations in main.cpp where the archiving/ restoring is performed:
BOOST_SERIALIZATION_SHARED_PTR( Building ) BOOST_SERIALIZATION_SHARED_PTR( House ) BOOST_SERIALIZATION_SHARED_PTR( Hospital )
In main.ccp, I would like to serialize a std::vector< boost::shared_ptr< Building > > object. Like this:
std::vector< boost::shared_ptr< Building > > buildings; ... std::ofstream ofs( "archive.txt" ); boost::archive::text_oarchive oa( ofs ); oa & BOOST_SERIALIZATION_NVP( buildings );
This compiles correctly but fails at runtime with a "unregistered_class" exception. The individual classes (Building, House, Hospital) can be serialized and restored fine individually. That is true for objects, pointers and shared_ptr. A vector of plain pointers to any of those types also works fine. But a vector of shared_ptr to any of those types fails at runtime..
What kind of registration must be done to achieve this?
_______________________________________________ Boost-users mailing list Boost-us...@lists.boost.orghttp://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-us...@lists.boost.orghttp://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Benoit Gagnon
-
Robert Ramey
-
Sohail Somani