|
Boost : |
From: Vladimir Prus (ghost_at_[hidden])
Date: 2003-09-29 01:40:04
The serialization library likes if serialized classes have default
constructor. Seems like it's possible to avoid declaring default ctor with
separate "load"/"save" functions, but this is not really necessary: I can put
default ctor in private section, and everything works ok.
The problem arises when deserializing vector of classes which private default
ctor. Even though class has "friend class boost::serialization::access", I
get compile errors.
The code is:
#include <boost/serialization/access.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
#include <vector>
using namespace std;
class V {
public:
V(int i) : m_i(i) {}
private:
int m_i;
private: // serialization support
friend class boost::serialization::access;
V() {}
template<class Archive>
void serialize(Archive& ar, unsigned version)
{
ar & m_i;
}
};
int main()
{
stringstream ss;
boost::archive::text_oarchive oa(ss);
vector<V> v;
oa << v;
boost::archive::text_iarchive ia(ss);
ia >> v;
return 0;
}
And the errors are:
vload.cpp: In member function `void
boost::serialization::stl::archive_input_seq<Archive,
Container>::operator()(Archive&, Container&) [with Archive =
boost::archive::basic_text_iarchive<std::istream>, Container =
std::vector<V, std::allocator<V> >]':
/home/ghost/Work/boost/boost/serialization/collections_load_imp.hpp:98:
instantiated from `void boost::serialization::stl::load_collection(Archive&,
Container&) [with Archive =
boost::archive::basic_text_iarchive<std::istream>, Container = std::vector<V,
std::allocator<V> >, InputFunction =
boost::serialization::stl::archive_input_seq<boost::archive::basic_text_iarchive<std::istream>,
std::vector<V, std::allocator<V> > >, R =
boost::serialization::stl::reserve_imp<std::vector<V, std::allocator<V> > >]'
/home/ghost/Work/boost/boost/serialization/vector.hpp:40: instantiated from
`void boost::serialization::load(Archive&, std::vector<U, Allocator>&,
unsigned int) [with Archive =
boost::archive::basic_text_iarchive<std::istream>, U = V, Allocator =
std::allocator<V>]'
/home/ghost/Work/boost/boost/serialization/split_free.hpp:45: instantiated
from `static void boost::serialization::detail::loader<Archive,
T>::invoke(Archive&, T&, unsigned int) [with Archive =
boost::archive::basic_text_iarchive<std::istream>, T = std::vector<V,
std::allocator<V> >]'
/home/ghost/Work/boost/boost/serialization/split_free.hpp:56: instantiated
from `void boost::serialization::split_free(Archive&, T&, unsigned int) [with
Archive = boost::archive::basic_text_iarchive<std::istream>, T =
std::vector<V, std::allocator<V> >]'
/home/ghost/Work/boost/boost/serialization/vector.hpp:57: instantiated from
`void boost::serialization::serialize(Archive&, std::vector<U, Allocator>&,
unsigned int, int) [with Archive =
boost::archive::basic_text_iarchive<std::istream>, U = V, Allocator =
std::allocator<V>]'
/home/ghost/Work/boost/boost/serialization/iserializer.hpp:63: instantiated
from `void boost::serialization::iserializer<Archive,
T>::load_object_data(boost::archive::basic_iarchive&, void*, unsigned int)
const [with Archive = boost::archive::basic_text_iarchive<std::istream>, T =
std::vector<V, std::allocator<V> >]'
/home/ghost/Work/boost/boost/archive/basic_text_oarchive.hpp:160:
instantiated from here
vload.cpp:21: error: `V::V()' is private
/home/ghost/Work/boost/boost/serialization/collections_load_imp.hpp:36: error:
within
this context
I've worked around this problem by using access::create in
collections_load_imp.hpp (after making the 'create' method public). But would
be great if this is solved more elegangly.
- Volodya
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk