
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?