newbie problem with Boost.Serialization

Hi all, I have some trouble with the Boost.Serialization library. I have compiled the 'demo.cpp' example, and now I am trying to distill it a little, so that I can understand how to apply it to my own polymorphic class hierarchy. However I keep seeing the same error message, and can't work out what it means. In /usr/include/boost/archive/detail/oserializer.hpp:567 there are some comments about the error message I'm seeing but I don't understand what those comments mean. I'm using Boost 1.33.1-7ubuntu1 on Ubuntu 6.10. Any help would be much appreciated! Cheers JP Here is the error message: --------------8<--------------- jpye@john:~/boosttest$ scons scons: Reading SConscript files ... scons: done reading SConscript files. scons: Building targets ... g++ -o ser1.o -c ser1.cpp /usr/include/boost/archive/detail/oserializer.hpp: In function 'void boost::archive::save(Archive&, T&) [with Archive = boost::archive::text_oarchive, T = dd]': /usr/include/boost/archive/basic_text_oarchive.hpp:78: instantiated from 'void boost::archive::basic_text_oarchive<Archive>::save_override(T&, int) [with T = dd, Archive = boost::archive::text_oarchive]' /usr/include/boost/archive/detail/interface_oarchive.hpp:78: instantiated from 'Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = dd, Archive = boost::archive::text_oarchive]' ser1.cpp:43: instantiated from here /usr/include/boost/archive/detail/oserializer.hpp:567: error: invalid application of 'sizeof' to incomplete type 'boost::STATIC_ASSERTION_FAILURE<false>' scons: *** [ser1.o] Error 1 scons: building terminated because of errors. jpye@john:~/boosttest$ -------------------------------------- Here is the code that was being compiled: -----------------8<--------------- jpye@john:~/boosttest$ cat ser1.cpp #include <iomanip> #include <iostream> #include <fstream> #include <string> #include <boost/archive/tmpdir.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/utility.hpp> #include <boost/serialization/list.hpp> class dd{ friend std::ostream&operator<<(std::ostream&,const dd&); friend class boost::serialization::access; double x,y; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */){ ar & y & x; } public: // every serializable class needs a constructor dd(){}; dd(double x, double y) : x(x), y(y) {} }; std::ostream & operator<<(std::ostream &os, const dd &d) { return os << d.x << ' ' << d.y; } int main(int argc, char *argv[]){ dd D(10,20); int x = 5; std::cerr << D << std::endl; std::ofstream ofs("test.txt"); boost::archive::text_oarchive oa(ofs); oa << x; oa << D; return 0; } -------------------------------------- And, just for completeness, my build file: --------------8<---------------- jpye@ascendserver:~/boosttest$ cat SConstruct Program("test",["ser1.cpp"], LIBS=['boost_serialization']) ------------------------------------

John Pye wrote:
Hi all,
I have some trouble with the Boost.Serialization library. I have compiled the 'demo.cpp' example, and now I am trying to distill it a little, so that I can understand how to apply it to my own polymorphic class hierarchy. However I keep seeing the same error message, and can't work out what it means.
In /usr/include/boost/archive/detail/oserializer.hpp:567 there are some comments about the error message I'm seeing but I don't understand what those comments mean. I'm using Boost 1.33.1-7ubuntu1 on Ubuntu 6.10.
Try the following change:
int main(int argc, char *argv[]){
const dd D(10,20); // note: const const int x = 5; // note const
std::cerr << D << std::endl;
std::ofstream ofs("test.txt"); boost::archive::text_oarchive oa(ofs); oa << x; oa << D;
return 0; }
Read the "rationale" section of the documentation for a complete expanation. Robert Ramey

Hi Robert, Robert Ramey wrote:
John Pye wrote:
However I keep seeing the same error message, and can't work out what it means.
Try the following change:
int main(int argc, char *argv[]){
const dd D(10,20); // note: const const int x = 5; // note const
OK, so objects-to-be-serialised must always be const. I didn't understand the rationale but I'll accept that there a good-but-obscure reason for it. I have a problem now with the BOOST_IS_ABSTRACT(ClassName) macro. What header file(s) must I include in order for that macro to behave correctly? Note that I am inside a namespace{...}when I write this macro. Should that be OK? Finally, if my base class is abstract and also contains no data requiring serialisation (I guess it's a base class for a Composite design pattern) then is it OK that my serialize(...) routine is empty? Cheers JP My output: ------8<------- object.h:56: error: ‘is_abstract’ is not a template object.h:56: error: explicit specialization of non-template ‘optx::boost::serialization::is_abstract’ object.h:56: error: ‘mpl’ has not been declared object.h:56: error: ISO C++ forbids declaration of ‘bool_’ with no type object.h:56: error: typedef name may not be a nested-name-specifier object.h:56: error: expected ‘;’ before ‘<’ token ... -------------- My header file: ------8<-------- ... #include <boost/archive/tmpdir.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/utility.hpp> #include <boost/serialization/list.hpp> namespace optx{ class Point; /** Class to hold a generic 3-D object. Basic geometric operations of rotation and translation will be provided as virtual methods. */ class Object{ ... ---------------

John Pye wrote:
What header file(s) must I include in order for that macro to behave correctly? Note that I am inside a namespace{...}when I write this macro. Should that be OK?
nope - all macros should be invoked from outside any namespaces.
Finally, if my base class is abstract and also contains no data requiring serialisation (I guess it's a base class for a Composite design pattern) then is it OK that my serialize(...) routine is empty?
That's ok. You can also eliminate the need for the serialize(...) in the abstract base class by registering the base/derived pair explicitly. This is also described in the manual. Its sort of an obscure point so it doesn't pop out. Robert Ramey
participants (2)
-
John Pye
-
Robert Ramey