Hi there,
I am relatively new to Boost in general and serialization in particular.
Nevertheless, having spent the day trying to find out how to serialize a
shared_ptr, I feel it is time to search for some external advice. So any
help is appreciated!
Here is the (hopefully) simple program:
/************************************************************/
#include <typeinfo>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include
#include
#include
#include
#include
#include
#include
// #include
#include
#include
#include
#include
#include
using namespace std;
using namespace boost;
using namespace boost::serialization;
class ptr{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version){
using boost::serialization::make_nvp;
ar & make_nvp("p",p);
}
shared_ptr<int> p;
public:
ptr(){
shared_ptr<int> dummy(new int(6));
p = dummy;
}
void printInfo(void){
cout << "*(p.get()) = " << *(p.get()) << endl;
}
};
main()
{
ostringstream ofs;
ptr myp;
{
boost::archive::xml_oarchive oa(ofs);
oa << make_nvp("myp",myp);
}
cout << ofs.str() << endl;
ptr newmyp;
{
istringstream ifs;
ifs.str(ofs.str());
boost::archive::xml_iarchive ia(ifs);
ia >> make_nvp("myp",newmyp);
}
newmyp.printInfo();
return 0;
}
/************************************************************/
When I try to compile this (OpenSUSE 10.2, g++ 4.1.2, Boost 1.33.1), I get a
LONG list of error messages, of which I will only quote the first few lines
only:
/************************************************************/
$ g++ -o test14 -lboost_serialization test14.cc
/usr/include/boost/serialization/shared_ptr.hpp: In function ‘void
boost::serialization::serialize(Archive&, boost::shared_ptr<U>&, unsigned
int) [with Archive = boost::archive::xml_iarchive, T = int]’:
/usr/include/boost/serialization/serialization.hpp:140: instantiated
from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int)
[with Archive = boost::archive::xml_iarchive, T = boost::shared_ptr<int>]’
/usr/include/boost/archive/detail/iserializer.hpp:160: instantiated
from ‘void boost::archive::detail::iserializer::load_object_data(boost::archive::detail::basic_iarchive&, void*,
unsigned int) const [with Archive = boost::archive::xml_iarchive, T =
boost::shared_ptr<int>]’
test14.cc:73: instantiated from here
/usr/include/boost/serialization/shared_ptr.hpp:235: error: invalid
application of ‘sizeof’ to incomplete
type ‘boost::STATIC_ASSERTION_FAILURE<false>’
[...]
/************************************************************/
The "STATIC_ASSERTION_FAILURE appears quite often in the error messages.
BTW, the program runs nicely, if I include a simple int as "data load" in
this class (and modify the program to accept an int instead of a
shared_ptr, of course). I'm therefore confident that most of the logic is
just fine.
Any help is appreciated!
Best Regards,
Ruediger