Boost logo

Boost Users :

Subject: Re: [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-19 13:26:08


On Mar 19, 9:58 am, "Robert Ramey" <ra..._at_[hidden]> 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?

>
>
>
> 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..._at_[hidden]http://lists.boost.org/mailman/listinfo.cgi/boost-users


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