Boost logo

Boost Users :

From: Richard Jennings (Richard.Jennings_at_[hidden])
Date: 2005-06-10 09:07:29


I am using Boost 1.32.0 with Borland C++ Builder 6 and attempting to
serialize an object via a pointer to a base class but I am getting a
runtime exception of "unregistered void cast".
 
Having read the page in the documentation on Runtime Casting, I thought
I should be able to use boost::serialization::void_cast_register<Base,
Derived>(0,0); to associate the base and derived classes, but I can't
get it to work. If I use the alternate method (to serialize the base
class and provide a do-nothing serialize method in the base class) then
it works fine. Any ideas what's wrong?

The program below demonstrates the problem. Class A uses the serialize
base class method and that's ok while class B uses the
void_cast_register method and an exception is thrown when it's
serialized:
-----
#include <iostream>
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/base_object.hpp>

#if defined(_DEBUG)
# pragma link "libboost_serialization-bcb-mt-d-1_32.lib"
#else
# pragma link "libboost_serialization-bcb-mt-1_32.lib"
#endif

class Base_c
{
  public:
    virtual void SomeMethod() = 0;
  private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {}
};
BOOST_IS_ABSTRACT(Base_c)

class A : public Base_c
{
  public:
    A() : m_Id("A"){};
    void SomeMethod(){};
  private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
/* boost::serialization::void_cast_register<Base_c, A>(0,0); */
      ar & boost::serialization::make_nvp("Base",
boost::serialization::base_object<Base_c>(*this));
      ar & BOOST_SERIALIZATION_NVP(m_Id);
    }
    std::string const m_Id;
};
BOOST_CLASS_EXPORT(A);

class B : public Base_c
{
  public:
    B() : m_Id("B"){};
    void SomeMethod(){};

  private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
      boost::serialization::void_cast_register<Base_c, B>(0,0);
/* ar & boost::serialization::make_nvp("Base",
boost::serialization::base_object<Base_c>(*this)); */
      ar & BOOST_SERIALIZATION_NVP(m_Id);
    }
    std::string const m_Id;
};
BOOST_CLASS_EXPORT(B)

main(int, char**)
{

  boost::filesystem::ofstream
OfStream(boost::filesystem::path("D:\\Temp\\SerialisationText.xml",
boost::filesystem::native));
  boost::archive::xml_oarchive Archive(OfStream);

  try
  {
    Base_c *pBase, *pBase2;
    pBase = new A();
    pBase2 = new B();

    Archive & boost::serialization::make_nvp("AByBasePointer", pBase);
    std::cout << "Serialised A ok\n";
    Archive & boost::serialization::make_nvp("BByBasePointer", pBase2);
    std::cout << "Serialised B ok\n";
  }
  catch (std::exception& E)
  {
    std::cout << "Exception: " << E.what();
  }
}
-----
The output is:
Serialised A ok
Exception: unregistered void cast


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