Boost logo

Boost Users :

From: Ruediger Berlich (ruediger.berlich_at_[hidden])
Date: 2007-03-30 21:49:52


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 <boost/shared_ptr.hpp>

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/vector.hpp>
// #include <boost/serialization/shared_ptr_132.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/tracking.hpp>

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<Archive,
T>::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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net