Re: [Boost-users] serialization: loading std::map replaces existingmapdata objects
Robert Ramey
Oh then maybe you want:
void serialize(boost::archive::xml_iarchive & ar, std::map
> & 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
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
wrote: Oh then maybe you want:
void serialize(boost::archive::xml_iarchive & ar, std::map
& 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
#include // For std::pair typedef boost::archive::xml_wiarchive iarchive; typedef std::wifstream ifstream;
struct Mark {}; enum MarkId {low,median,high}; typedef std::map
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
&, 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 ]' /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:158: instantiated from `void boost::archive::detail::iserializer ::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::xml_wiarchive, T = std::pair ]' /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 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
&, 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 ]' /usr/local/include/boost-1_33/boost/archive/detail/iserializer.hpp:158: instantiated from `void boost::archive::detail::iserializer ::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::xml_wiarchive, T = std::pair ]' /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
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
Try putting it in either the namespace of std::map
Robert Ramey
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
#include // For std::pair struct Mark {}; enum MarkId {low,median,high}; typedef std::map
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
Try putting it in either the namespace of std::map
(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