|
Boost Users : |
Subject: [Boost-users] Serialization of a std::vector< boost::shared_ptr< A > > throws unregistered_class exception
From: Benoit Gagnon (b.gagnon_at_[hidden])
Date: 2009-03-18 20:46:09
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 list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net