Boost logo

Boost Users :

From: Daryle Walker (darylew_at_[hidden])
Date: 2005-12-04 14:08:42


On 12/3/05 10:56 PM, "David Parks" <davidp_at_[hidden]> wrote:

> I'm having a newbie issue with serialization on a derived class with a
> virtual function. I'm using the 1.33.0 build on VS 7.1.
> I've shrunken my code to a micro-version of what I tried to do. Here I
> have two classes, A and B, where B is a subclass of A. They share a
> virtual function (ie B overrides dumpFunc in A). I've registered B with
> BOOST_CLASS_EXPORT(B). I've turned on RTTI in VS 7.1 (/GR or
> whatever). Still, I get a run time assert error in the boost library
> telling me that the derived/base relationship wasn't registered. Here's
> the code. I'll try to spell out the error below.

Why don't you follow the error's advice and register the derived/base
relationship? The registration is not automatic.

> #include <string>
> #include <fstream>
>
> #include <boost/archive/tmpdir.hpp>
>
> #include <boost/archive/text_iarchive.hpp>
> #include <boost/archive/text_oarchive.hpp>
> #include <boost/serialization/export.hpp>
> #include <boost/serialization/base_object.hpp>

You already included the necessary header, but you don't exploit it.

> #include <boost/serialization/utility.hpp>
> #include <boost/serialization/vector.hpp>
>
> class A;
> class B;
>
> class A
> {
> friend class boost::serialization::access;
>
> template<class Archive>
> void serialize(Archive & ar, const unsigned int version)
> {
> ar & m_className;
> };
> std::string m_className;
> public:
> A()
> {
> m_className = "ClassA\n";
> };
> virtual void dumpFunc() {};
> };
>
>
> class B : public A
> {
> friend class boost::serialization::access;
>
> template<class Archive>
> void serialize(Archive & ar, const unsigned int version)
> {

       // Add this line to acknowledge base classes
       ar & boost::serialization::base_object<A>(*this);

> ar & m_classNameB;
> };
> std::string m_classNameB;
> public:
> B()
> {
> m_classNameB = "ClassB\n";
> };
> virtual void dumpFunc() {};
> };
>
> BOOST_CLASS_EXPORT(B)
>
> void main()
> {
> std::vector<A *> vecA;
> B *b=new B();
> vecA.push_back(b);
>
> std::ofstream ofs("saveFile.sav");
> boost::archive::text_oarchive oa(ofs);
> oa & vecA;
>
> delete b;
> }
[TRUNCATE stack trace]

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

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