Some questions about using BOOST_CLASS_EXPORT_KEY macro of boost.serialization library
Dear all , The library version I used was boost_1_42, and the IDE was VS2008 . When I attempt to use the BOOST_CLASS_EXPORT_KEY macro, I met some problems. In my word, I think it doesn't work as expected . My project files are listed following (just five tiny files) : // Base.h #ifndef _BASE_H #define _BASE_H #include <boost/serialization/access.hpp> #include <boost/serialization/export.hpp> #include <boost/serialization/type_info_implementation.hpp> #include <boost/serialization/extended_type_info_no_rtti.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/shared_ptr.hpp> class Base { public: Base() ; Base(int a) ; virtual ~Base(); private: friend class boost::serialization::access ; template<class Archive> void serialize(Archive& ar, const unsigned int version) { ar & a_ ; } private: int a_ ; }; BOOST_CLASS_EXPORT_KEY(Base) BOOST_SERIALIZATION_SHARED_PTR(Base) #endif //_BASE_H // Base.cpp #include "Base.h" Base::Base(): a_(0){ } Base::Base( int a ): a_(a){ } Base::~Base(){ } BOOST_CLASS_EXPORT_IMPLEMENT(Base) //Derived.h #ifndef _DERIVED_H #define _DERIVED_H #include "base.h" class Derived : public Base { public: Derived(); Derived(int a, int b); ~Derived(); private: friend class boost::serialization::access ; template<class Archive> void serialize(Archive& ar, const unsigned int version) { ar & boost::serialization::base_object<Base>(*this) ; ar & b_ ; } private: int b_ ; }; BOOST_CLASS_EXPORT_KEY(Derived) BOOST_SERIALIZATION_SHARED_PTR(Derived) #endif // _DERIVED_H // Drived.cpp #include "StdAfx.h" #include "Derived.h" Derived::Derived(): b_(0){ } Derived::Derived( int a, int b ): Base(a), b_(b){ } Derived::~Derived(){ } BOOST_CLASS_EXPORT_IMPLEMENT(Derived) // main.cpp #include <fstream> #include <iostream> #include <boost/archive/text_oarchive.hpp> #include <boost/serialization/shared_ptr.hpp> #include "Derived.h" int _tmain(int argc, _TCHAR* argv[]) { boost::shared_ptr<Base> base_ptr(new Derived(10,15)) ; std::ofstream ofs("test.txt") ; boost::archive::text_oarchive oa(ofs) ; oa << base_ptr ; return 0; } There are no errors in compile-time. But when I executed the program, a memory error caused ! I don't know what is wrong about my program. Whether my usage of BOOST_CLASS_EXPORT_KEY/BOOST_CLASS_EXPORT_IMPLEMENT was wrong ? And, how can I fix the problems ? Thanks, and with my best wishes. Lei
Igore Dmit. wrote:
2010/2/26 ZouLei <zoulei.vresearch.org@gmail.com>
oa << base_ptr ;
I rechecked documentation and I believe it should look like "oa << *base_ptr ;
". Correct me if I'm wrong. You're wrong - where in the documentation does it say this? Robert Ramey
Yes, my fault... should really go to bed. Sorry again, spotted "gps" examples, and somewhy decided passing by value could help... 2010/2/26 Robert Ramey <ramey@rrsd.com>
Igore Dmit. wrote:
2010/2/26 ZouLei <zoulei.vresearch.org@gmail.com>
oa << base_ptr ;
I rechecked documentation and I believe it should look like "oa << *base_ptr ;
". Correct me if I'm wrong.
You're wrong - where in the documentation does it say this?
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
But, how can I resolve the problem ? I mean that where is the correct way to use BOOST_CLASS_EXPORT_KEY/BOOST_CLASS_EXPORT_IMPLEMENT macros ? 2010/2/27 Igore Dmit. <trueorca@gmail.com>
Yes, my fault... should really go to bed. Sorry again, spotted "gps" examples, and somewhy decided passing by value could help...
2010/2/26 Robert Ramey <ramey@rrsd.com>
Igore Dmit. wrote:
2010/2/26 ZouLei <zoulei.vresearch.org@gmail.com>
oa << base_ptr ;
I rechecked documentation and I believe it should look like "oa << *base_ptr ;
". Correct me if I'm wrong.
You're wrong - where in the documentation does it say this?
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Igore Dmit.
-
Robert Ramey
-
ZouLei
-
邹磊