Re: [Boost-users] serialization: loading std::map replaces existingmapdata objects

Robert Ramey <ramey@rrsd.com> wrote:
Oh then maybe you want:
void serialize(boost::archive::xml_iarchive & ar, std::map<my_class> > & t, const unsigned int version){ // for each member in the map ar & *it; }
Ah, I was looking for that but missed it. I'm having trouble compiling the load though as gcc doesnt like the map member's std::pair.first being const. I'm sure I'm doing something incorrect that's blindingly obvious, but I just can't see it. The code and compiler messages are below. #include <map> #include <fstream> #include <boost/archive/xml_wiarchive.hpp> #include <boost/serialization/utility.hpp> // For std::pair typedef boost::archive::xml_wiarchive iarchive; typedef std::wifstream ifstream; struct Mark {}; enum MarkId {low,median,high}; typedef std::map<MarkId,Mark> MarkMap; void serialize(iarchive &ar, MarkMap & m, const unsigned int version) { MarkMap::iterator it; for (it = m.begin() ; it != m.end(); ++it) { ar & boost::serialization::make_nvp("item", *it); } } int main(int argc, char* argv[]) { ifstream ifs; iarchive ar(ifs); MarkMap marks; ar >> boost::serialization::make_nvp("marks",marks); } gcc --version gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5) g++ -c tsrz.cpp -I/usr/local/include -isystem /usr/local/include/boost-1_33 -isystem ./boost -g -Wall -Wno-non-virtual-dtor -Wno-inline -ftemplate-depth-255 -finline-functions -MMD -MF.deps/tsrz.d /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp: In function `void boost::archive::load(Archive&, T&) [with Archive = boost::archive::xml_wiarchive, T = const MarkId]': /usr/local/include/boost-1_33/boost/archive/basic_xml_iarchive.hpp:80: instantiated from `void boost::archive::basic_xml_iarchive<Archive>::load_override(const boost::serialization::nvp<T>&, int) [with T = const MarkId, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/xml_wiarchive.hpp:78: instantiated from `void boost::archive::xml_wiarchive_impl<Archive>::load_override(T&, int) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/detail/interface_iarchive.hpp:84: instantiated from `Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/detail/interface_iarchive.hpp:91: instantiated from `Archive& boost::archive::detail::interface_iarchive<Archive>::operator&(T&) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/serialization/utility.hpp:43: instantiated from `void boost::serialization::serialize(Archive&, std::pair<F, S>&, unsigned int) [with Archive = boost::archive::xml_wiarchive, F = const MarkId, S = Mark]' /usr/local/include/boost-1_33/boost/serialization/serialization.hpp:140: instantiated from `void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_wiarchive, T = std::pair<const MarkId, Mark>]' /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:158: 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_wiarchive, T = std::pair<const MarkId, Mark>]' /usr/local/include/boost-1_33/boost/mpl/if.hpp:67: instantiated from here /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:544: `sizeof' applied to incomplete type `boost::STATIC_ASSERTION_FAILURE<false>' /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp: In static member function `static void boost::archive::detail::load_enum_type<Archive, T>::invoke(Archive&, T&) [with Archive = boost::archive::xml_wiarchive, T = const MarkId]': /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:559: instantiated from `void boost::archive::load(Archive&, T&) [with Archive = boost::archive::xml_wiarchive, T = const MarkId]' /usr/local/include/boost-1_33/boost/archive/basic_xml_iarchive.hpp:80: instantiated from `void boost::archive::basic_xml_iarchive<Archive>::load_override(const boost::serialization::nvp<T>&, int) [with T = const MarkId, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/xml_wiarchive.hpp:78: instantiated from `void boost::archive::xml_wiarchive_impl<Archive>::load_override(T&, int) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/detail/interface_iarchive.hpp:84: instantiated from `Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/detail/interface_iarchive.hpp:91: instantiated from `Archive& boost::archive::detail::interface_iarchive<Archive>::operator&(T&) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/serialization/utility.hpp:43: instantiated from `void boost::serialization::serialize(Archive&, std::pair<F, S>&, unsigned int) [with Archive = boost::archive::xml_wiarchive, F = const MarkId, S = Mark]' /usr/local/include/boost-1_33/boost/serialization/serialization.hpp:140: instantiated from `void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_wiarchive, T = std::pair<const MarkId, Mark>]' /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:158: 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_wiarchive, T = std::pair<const MarkId, Mark>]' /usr/local/include/boost-1_33/boost/mpl/if.hpp:67: instantiated from here /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:491: assignment of read-only reference `t'

Which version of boost are you using? I believe this may have come up and been addressed in 1_33_1? Robert Ramey Peter Whaite wrote:
Robert Ramey <ramey@rrsd.com> wrote:
Oh then maybe you want:
void serialize(boost::archive::xml_iarchive & ar, std::map<my_class>
& t, const unsigned int version){ // for each member in the map ar & *it; }
Ah, I was looking for that but missed it. I'm having trouble compiling the load though as gcc doesnt like the map member's std::pair.first being const. I'm sure I'm doing something incorrect that's blindingly obvious, but I just can't see it. The code and compiler messages are below.
#include <map> #include <fstream> #include <boost/archive/xml_wiarchive.hpp> #include <boost/serialization/utility.hpp> // For std::pair
typedef boost::archive::xml_wiarchive iarchive; typedef std::wifstream ifstream;
struct Mark {}; enum MarkId {low,median,high}; typedef std::map<MarkId,Mark> MarkMap;
void serialize(iarchive &ar, MarkMap & m, const unsigned int version) { MarkMap::iterator it; for (it = m.begin() ; it != m.end(); ++it) { ar & boost::serialization::make_nvp("item", *it); } }
int main(int argc, char* argv[]) { ifstream ifs; iarchive ar(ifs); MarkMap marks; ar >> boost::serialization::make_nvp("marks",marks); }
gcc --version gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
g++ -c tsrz.cpp -I/usr/local/include -isystem /usr/local/include/boost-1_33 -isystem ./boost -g -Wall -Wno-non-virtual-dtor -Wno-inline -ftemplate-depth-255 -finline-functions -MMD -MF.deps/tsrz.d /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp: In function `void boost::archive::load(Archive&, T&) [with Archive = boost::archive::xml_wiarchive, T = const MarkId]': /usr/local/include/boost-1_33/boost/archive/basic_xml_iarchive.hpp:80: instantiated from `void boost::archive::basic_xml_iarchive<Archive>::load_override(const boost::serialization::nvp<T>&, int) [with T = const MarkId, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/xml_wiarchive.hpp:78: instantiated from `void boost::archive::xml_wiarchive_impl<Archive>::load_override(T&, int) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/detail/interface_iarchive.hpp:84: instantiated from `Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/detail/interface_iarchive.hpp:91: instantiated from `Archive& boost::archive::detail::interface_iarchive<Archive>::operator&(T&) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/serialization/utility.hpp:43: instantiated from `void boost::serialization::serialize(Archive&, std::pair<F, S>&, unsigned int) [with Archive = boost::archive::xml_wiarchive, F = const MarkId, S = Mark]' /usr/local/include/boost-1_33/boost/serialization/serialization.hpp:140: instantiated from `void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_wiarchive, T = std::pair<const MarkId, Mark>]' /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:158: 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_wiarchive, T = std::pair<const MarkId, Mark>]' /usr/local/include/boost-1_33/boost/mpl/if.hpp:67: instantiated from here /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:544: `sizeof' applied to incomplete type `boost::STATIC_ASSERTION_FAILURE<false>' /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp: In static member function `static void boost::archive::detail::load_enum_type<Archive,
invoke(Archive&, T&) [with Archive = boost::archive::xml_wiarchive, T = const MarkId]': /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:559: instantiated from `void boost::archive::load(Archive&, T&) [with Archive = boost::archive::xml_wiarchive, T = const MarkId]' /usr/local/include/boost-1_33/boost/archive/basic_xml_iarchive.hpp:80: instantiated from `void boost::archive::basic_xml_iarchive<Archive>::load_override(const boost::serialization::nvp<T>&, int) [with T = const MarkId, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/xml_wiarchive.hpp:78: instantiated from `void boost::archive::xml_wiarchive_impl<Archive>::load_override(T&, int) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/detail/interface_iarchive.hpp:84: instantiated from `Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/archive/detail/interface_iarchive.hpp:91: instantiated from `Archive& boost::archive::detail::interface_iarchive<Archive>::operator&(T&) [with T = const boost::serialization::nvp<const MarkId>, Archive = boost::archive::xml_wiarchive]' /usr/local/include/boost-1_33/boost/serialization/utility.hpp:43: instantiated from `void boost::serialization::serialize(Archive&, std::pair<F, S>&, unsigned int) [with Archive = boost::archive::xml_wiarchive, F = const MarkId, S = Mark]' /usr/local/include/boost-1_33/boost/serialization/serialization.hpp:140: instantiated from `void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_wiarchive, T = std::pair<const MarkId, Mark>]' /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:158: 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_wiarchive, T = std::pair<const MarkId, Mark>]' /usr/local/include/boost-1_33/boost/mpl/if.hpp:67: instantiated from here /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:491: assignment of read-only reference `t'

Robert Ramey <ramey@rrsd.com> wrote:
Which version of boost are you using? I believe this may have come up and been addressed in 1_33_1?
Now that spirit is fixed I have tried again on the CVS (1_34). Unfortunately I now have a different error -- it prefers the serialize template over my serialize function? #include <map> #include <fstream> #include <boost/archive/xml_wiarchive.hpp> #include <boost/serialization/utility.hpp> // For std::pair struct Mark {}; enum MarkId {low,median,high}; typedef std::map<MarkId,Mark> MarkMap; void serialize(boost::archive::xml_wiarchive & ar, MarkMap & m, const unsigned int version) { MarkMap::iterator it; for (it = m.begin() ; it != m.end(); ++it) { ar & boost::serialization::make_nvp("item", *it); } } int main(int argc, char* argv[]) { std::wifstream ifs; boost::archive::xml_wiarchive ar(ifs); MarkMap marks; ar & boost::serialization::make_nvp("marks",marks); } % g++ -v Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux Thread model: posix gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) % make -k tsrz g++ -c tsrz.cpp -I/usr/local/include -isystem /usr/local/include/boost-1_34 -isystem ./boost -g -Wall -Wno-non-virtual-dtor -Wno-inline -ftemplate-depth-255 -finline-functions -MMD -MF.deps/tsrz.d /usr/local/include/boost-1_34/boost/serialization/access.hpp: In static member function `static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_wiarchive, T = Mark]': /usr/local/include/boost-1_34/boost/serialization/serialization.hpp:81: instantiated from `void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_wiarchive, T = Mark]' /usr/local/include/boost-1_34/boost/serialization/serialization.hpp:140: instantiated from `void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_wiarchive, T = Mark]' /usr/local/include/boost-1_34/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_wiarchive, T = Mark]' /usr/include/c++/3.2.2/bits/locale_facets.h:111: instantiated from here /usr/local/include/boost-1_34/boost/serialization/access.hpp:109: no matching function for call to `Mark::serialize(boost::archive::xml_wiarchive&, const unsigned int&)' make: *** [.deps/tsrz.d] Error 1 make: Target `tsrz' not remade because of errors. -- Peter Whaite (http://whaite.ca)

Try putting it in either the namespace of std::map<MarkId, Mark> (ie std) or in the namespace boost::serialization Robert Ramey Peter Whaite wrote:
Robert Ramey <ramey@rrsd.com> wrote:
Which version of boost are you using? I believe this may have come up and been addressed in 1_33_1?
Now that spirit is fixed I have tried again on the CVS (1_34). Unfortunately I now have a different error -- it prefers the serialize template over my serialize function?
#include <map> #include <fstream> #include <boost/archive/xml_wiarchive.hpp> #include <boost/serialization/utility.hpp> // For std::pair
struct Mark {}; enum MarkId {low,median,high}; typedef std::map<MarkId,Mark> MarkMap;
void serialize(boost::archive::xml_wiarchive & ar, MarkMap & m, const unsigned int version) { MarkMap::iterator it; for (it = m.begin() ; it != m.end(); ++it) { ar & boost::serialization::make_nvp("item", *it); } }
int main(int argc, char* argv[]) { std::wifstream ifs; boost::archive::xml_wiarchive ar(ifs); MarkMap marks; ar & boost::serialization::make_nvp("marks",marks); }

Robert Ramey <ramey@rrsd.com> wrote:
Try putting it in either the namespace of std::map<MarkId, Mark> (ie std) or in the namespace boost::serialization
Sorry. Dumb mistake on my part from not carefully (painstakingly?) examining the error messages. The code was a minimal example to expose the original problem, and it is missing Mark::serialize. The MarkMap serialize function was found, and the namespace is not necessary. When I go back to the full code it works perfectly. Thanks once again for your prompt and useful responses.
participants (2)
-
Peter Whaite
-
Robert Ramey