Serialization v1.33.0 and Serializing derived classes through base class
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.
#include <string>
#include <fstream>
#include
junkProject.exe!boost::throw_exceptionboost::archive::archive_exception(const
boost::archive::archive_exception & e={...}) Line 40 C++
junkProject.exe!boost::archive::detail::save_pointer_type
::save_object_data(boost::archive::detail::basic_oarchive & ar={...}, const void * x=0x0012febc) Line 151 + 0x2b C++
junkProject.exe!boost::archive::detail::basic_oarchive_impl::save_object(boost::archive::detail::basic_oarchive
& ar={...}, const void * t=0x0012febc, const
boost::archive::detail::basic_oserializer & bos={...}) Line 271 C++
junkProject.exe!boost::archive::detail::basic_oarchive::save_object(const
void * x=0x0012febc, const boost::archive::detail::basic_oserializer &
bos={...}) Line 412 C++
junkProject.exe!boost::archive::detail::save_non_pointer_type
::save_standard::invoke(boost::archive::text_oarchive & ar={...}, const std::vector > & t={...}) Line 264 C++
junkProject.exe!boost::archive::detail::save_non_pointer_type
David Parks 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.
class B : public A { friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & m_classNameB; }; std::string m_classNameB; public: B() { ar & boost::serialization::base_object<A>(*this); //<<<<< inssert this ! m_classNameB = "ClassB\n"; }; virtual void dumpFunc() {}; };
This little detail isn't that obvious - even though it is described in the manual. Normally, derived classes need to call serialization of their parents. This fact is leveraged to "register" the relationship between a derived class and its parent which is used to serialization a derived class from a base class pointer. If this "base_object" isn't used, this relationship has to be made explicitly with "void_cast_register" from and even more obscure lower level. I made this change and verified that your program compiles and runs as you would hope. Robert Ramey
On 12/3/05 10:56 PM, "David Parks"
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
#include
#include #include #include
You already included the necessary header, but you don't exploit it.
#include
#include 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 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
participants (3)
-
Daryle Walker
-
David Parks
-
Robert Ramey