|
Boost Users : |
From: Luca Cappa (luca.cappa_at_[hidden])
Date: 2007-04-18 12:08:17
Hello,
I am boost-serializing some instances of polymorphic class using their
base class pointer.
I get the 'archive_exception::unregistered_cast' exception at runtime when
serializing one of them. The comment at line 407 of oserializer.hpp (where
the exception is thrown) "convert pointer to more derived type. if this is
thrown it means that the base/derived relationship hasn't be registered".
The code is here below. I think i registered correctly the derived
classes, but I have not been able to make it working: in few words, i
cannot serialize a Serializable class using a pointer to its base
SerializableBase base class. Any suggestion?
TIA,
Luca
using namespace std;
using namespace boost;
using namespace boost::serialization;
struct SerializableBase
{
virtual ~SerializableBase ()
{
}
template<typename Archive>
void serialize (Archive& pAR, unsigned int version)
{
int i (0);
pAR & make_nvp ("doesnotwork", i);
}
};
struct Serializable : public SerializableBase
{
int a;
Serializable () : a (1)
{
}
virtual ~Serializable ()
{
}
template<typename Archive>
void serialize (Archive& pAR, unsigned int version)
{
pAR & BOOST_SERIALIZATION_NVP (a);
pAR & make_nvp ("woowoo", b);
}
};//struct
void save (const Serializable& pS, const char* pFileName)
{
// make an archive
std::ofstream ofs (pFileName);
boost::archive::xml_oarchive oa (ofs);
// Registering a lot of things here!
oa.register_type (static_cast<Serializable*> (NULL));
oa.register_type (static_cast<SerializableBase*> (NULL));
oa.register_type<shared_ptr<SerializableBase> > ();
oa.register_type<shared_ptr<Serializable> > ();
oa.register_type<Serializable> ();
oa.register_type<SerializableBase> ();
SerializableBase* lS (new Serializable);
oa << make_nvp ("lS", lS);
}
int main ()
{
Serializable s;
save (s, "serializable.xml");
}
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