Terence Wilson wrote:
I have condensed a short example which is appended to this message. The code tries to write then read a polymorphic class via a base pointer. The assertion I am seeing is assert(NULL != bpos_ptr); in oserializer.hpp. The documentation indicates that the problem is likely a lack of registration of the class, however, I have done this. Interestingly the code only fails for XML archive type.
// BoostPolymorphicSerialize.cpp : Defines the entry point for the console application. //
#include
#include #include <iomanip> #include <iostream> #include <fstream> #include <string>
#include
#include
#include #include
#include #include #include #include
#include #include #include "boost/archive/xml_iarchive.hpp" #include "boost/archive/xml_oarchive.hpp"
class CMyBaseClass { public: CMyBaseClass(void):m_BaseValue(0){} public: virtual ~CMyBaseClass(void){} int m_BaseValue;
friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */) { ar & BOOST_SERIALIZATION_NVP(m_BaseValue); } };
class CMyDerivedClass : public CMyBaseClass { public: CMyDerivedClass(void):m_DerivedValue(1){} public: ~CMyDerivedClass(void){} int m_DerivedValue;
friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */) { BOOST_SERIALIZATION_BASE_OBJECT_NVP(CMyBaseClass); ar & BOOST_SERIALIZATION_NVP(m_DerivedValue); } };
BOOST_CLASS_EXPORT_GUID(CMyBaseClass, "CMyBaseClass") BOOST_CLASS_EXPORT_GUID(CMyDerivedClass, "CMyDerivedClass")
int _tmain(int argc, _TCHAR* argv[]) { const CMyBaseClass* const pMyBaseClass=new CMyDerivedClass(); const std::string fileName("c:\\docs\\BoostSerializeTest.xml"); CMyBaseClass* pMyBaseClass2=0;
try { { std::ofstream ofs(fileName.c_str()); assert(ofs.good()); boost::archive::xml_oarchive oa(ofs); //boost::archive::text_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(pMyBaseClass); }
{ std::ifstream ifs(fileName.c_str()); assert(ifs.good()); boost::archive::xml_iarchive ia(ifs); //boost::archive::text_iarchive ia(ifs); ia >> BOOST_SERIALIZATION_NVP(pMyBaseClass2); } } catch(std::exception& e) { std::cout << e.what() << std::endl; }
delete pMyBaseClass; delete pMyBaseClass2;
return 0; }