问题
I am new to boost & boost serialization. I am trying to de-serialize a STL map class member. Here is the code :
...
class Face
{
friend std::ostream & operator<<(std::ostream &os, const Face &mf);
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
ar & mFaceID;
}
public:
size_t mFaceID; /*!< an integer value represents the Face Identification number */
void setFaceId(const size_t& faceID)
{
mFaceID = faceID;
}
};
std::ostream & operator<<(std::ostream &os, const Face &mf)
{
return os << ' ' << (size_t) mf.mFaceID << ' ';
}
Point is a member of the derived class define below.
...
class Point
{
friend class boost::serialization::access;
friend std::ostream & operator<<(std::ostream &os, const Point &p);
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
// save/load base class information
ar & X & Y & Z;
}
public:
double X, Y, Z;
Point(double X, double Y, double Z)
: X(X), Y(Y), Z(Z)
{}
};
Derived class of Face
...
class Bend : public Face
{
friend class boost::serialization::access;
friend std::ostream & operator<<(std::ostream &os, const Bend &br);
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
// save/load base class information
ar & boost::serialization::base_object<Face>(*this);
ar & mBendAngle & mBendDirection;
ar & bendLinePnt & bendLineDir;
}
public:
double mBendAngle;
size_t mBendDirection;
Point bendLinePnt, bendLineDir;
Bend(const size_t& faceID, double angle, size_t b_dir, Point pnt, Point dir);
};
std::ostream & operator<<(std::ostream &os, const Bend &mb)
{
os << mb.mBendAngle << " " << mb.mBendDirection << " " << mb.bendLinePnt.X << " " << mb.bendLinePnt.Y << " " << mb.bendLinePnt.Z;
os << mb.bendLineDir.X << " " << mb.bendLineDir.Y << " " << mb.bendLineDir.Z;
os << '\n';
return os;
}
Bend::Bend(const size_t& faceID, double angle, size_t b_dir, Point pnt, Point dir)
: mBendAngle(angle), mBendDirection(b_dir), bendLinePnt(pnt), bendLineDir(dir)
{
setFaceId(faceID);
}
This class contains the map that I would like to de-serialize :
...
class Model
{
friend class boost::serialization::access;
friend std::ostream & operator<<(std::ostream &os, const Model &m);
template<class Archive>
void serialize(Archive &ar, Bend& bend, const unsigned int version)
{
ar & bends;
}
public:
std::map<const size_t, Bend> bends; // THIS IS THE MAP
void addBend(const Bend& bend);
void init();
};
std::ostream & operator<<(std::ostream &os, const Model &m)
{
for(auto it = m.bends.begin(); it != m.bends.end(); it++){
os << it->first << it->second;
}
return os;
}
void Model::addBend(const Bend& bend)
{
bends.insert({bend.mFaceID, bend});
}
void Model::init()
{
size_t faceID = 1;
size_t dir = 0;
Bend b1(faceID, 90, dir, Point(2, 4, 5), Point(3,5,6));
addBend(b1);
++faceID;
Bend b2(faceID, 90, dir + 1, Point(2, 4, 5), Point(3,5,6));
addBend(b2);
++faceID;
Bend b3(faceID, 90, dir, Point(2, 4, 5), Point(3,5,6));
addBend(b3);
++faceID;
Bend b4(faceID, 90, dir + 1, Point(2, 4, 5), Point(3,5,6));
addBend(b4);
}
Class Usage :
#include "model.h"
void save(const Model &m, const char * filename)
{
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << m;
}
void restore(Model &m, const char * filename)
{
// open the archive
std::ifstream ifs(filename);
boost::archive::text_iarchive ia(ifs);
// restore the model from the archive
ia >> m;
}
int main(int argc, char const *argv[])
{
auto test = std::make_unique<Model>();
test->init();
save(*test, "text_file.txt");
auto test2 = std::make_unique<Model>();
restore(*test2, "text_file.txt");
return 0;
}
The serialization process works Ok I need help with the restore() function or improvements on my code. When I run the code I get a long list of errors such as :
In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37,
from /usr/include/boost/archive/detail/iserializer.hpp:45,
from /usr/include/boost/archive/detail/interface_iarchive.hpp:22,
from /usr/include/boost/archive/detail/common_iarchive.hpp:23,
from /usr/include/boost/archive/basic_text_iarchive.hpp:30,
from /usr/include/boost/archive/text_iarchive.hpp:24,
from face.h:9,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/boost/serialization/access.hpp: In instantiation of ‘static void boost::serialization::access::construct(T*) [with T = std::pair<const long unsigned int, Bend>]’:
/usr/include/boost/serialization/serialization.hpp:92:22: required from ‘void boost::serialization::load_construct_data(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive; T = std::pair<const long unsigned int, Bend>]’
/usr/include/boost/serialization/serialization.hpp:148:24: required from ‘void boost::serialization::load_construct_data_adl(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive; T = std::pair<const long unsigned int, Bend>]’
/usr/include/boost/serialization/detail/stack_constructor.hpp:51:54: required from ‘boost::serialization::detail::stack_construct<Archive, T>::stack_construct(Archive&, unsigned int) [with Archive = boost::archive::text_iarchive; T = std::pair<const long unsigned int, Bend>]’
/usr/include/boost/serialization/map.hpp:60:48: required from ‘void boost::serialization::load_map_collection(Archive&, Container&) [with Archive = boost::archive::text_iarchive; Container = std::map<const long unsigned int, Bend>]’
/usr/include/boost/serialization/map.hpp:89:24: required from ‘void boost::serialization::load(Archive&, std::map<Key, Type, Compare, Allocator>&, unsigned int) [with Archive = boost::archive::text_iarchive; Type = Bend; Key = const long unsigned int; Compare = std::less<const long unsigned int>; Allocator = std::allocator<std::pair<const long unsigned int, Bend> >]’
/usr/include/boost/serialization/split_free.hpp:58:13: [ skipping 22 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/boost/archive/detail/iserializer.hpp:626:18: required from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::text_iarchive; T = Model]’
/usr/include/boost/archive/detail/common_iarchive.hpp:67:22: required from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&) [with T = Model; Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/basic_text_iarchive.hpp:70:9: required from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&) [with T = Model; Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/text_iarchive.hpp:82:52: required from ‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&) [with T = Model; Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:68:9: required from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = Model; Archive = boost::archive::text_iarchive]’
main.cpp:17:11: required from here
/usr/include/boost/serialization/access.hpp:130:9: error: no matching function for call to ‘std::pair<const long unsigned int, Bend>::pair()’
::new(t)T;
^~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/string:40,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/iomanip:40,
from face.h:2,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:436:9: note: candidate: ‘template<class ... _Args1, long unsigned int ..._Indexes1, class ... _Args2, long unsigned int ..._Indexes2> std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>)’
pair(tuple<_Args1...>&, tuple<_Args2...>&,
^~~~
/usr/include/c++/8/bits/stl_pair.h:436:9: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37,
from /usr/include/boost/archive/detail/iserializer.hpp:45,
from /usr/include/boost/archive/detail/interface_iarchive.hpp:22,
from /usr/include/boost/archive/detail/common_iarchive.hpp:23,
from /usr/include/boost/archive/basic_text_iarchive.hpp:30,
from /usr/include/boost/archive/text_iarchive.hpp:24,
from face.h:9,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/boost/serialization/access.hpp:130:9: note: candidate expects 4 arguments, 0 provided
::new(t)T;
^~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/string:40,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/iomanip:40,
from face.h:2,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:375:9: note: candidate: ‘template<class ... _Args1, class ... _Args2> std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>)’
pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
^~~~
/usr/include/c++/8/bits/stl_pair.h:360:12: note: candidate: ‘template<class _U1, class _U2, typename std::enable_if<(std::_PCC<((! std::is_same<const long unsigned int, _U1>::value) || (! std::is_same<Bend, _U2>::value)), const long unsigned int, Bend>::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<((! std::is_same<const long unsigned int, _U1>::value) || (! std::is_same<Bend, _U2>::value)), const long unsigned int, Bend>::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&)’
constexpr pair(pair<_U1, _U2>&& __p)
^~~~
/usr/include/c++/8/bits/stl_pair.h:360:12: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37,
from /usr/include/boost/archive/detail/iserializer.hpp:45,
from /usr/include/boost/archive/detail/interface_iarchive.hpp:22,
from /usr/include/boost/archive/detail/common_iarchive.hpp:23,
from /usr/include/boost/archive/basic_text_iarchive.hpp:30,
from /usr/include/boost/archive/text_iarchive.hpp:24,
from face.h:9,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/boost/serialization/access.hpp:130:9: note: candidate expects 1 argument, 0 provided
::new(t)T;
^~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/string:40,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/iomanip:40,
from face.h:2,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:350:21: note: candidate: ‘template<class _U1, class _U2, typename std::enable_if<(_MoveConstructiblePair<_U1, _U2>() && (! _ImplicitlyMoveConvertiblePair<_U1, _U2>())), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&)’
explicit constexpr pair(_U1&& __x, _U2&& __y)
^~~~
/usr/include/c++/8/bits/stl_pair.h:350:21: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37,
from /usr/include/boost/archive/detail/iserializer.hpp:45,
from /usr/include/boost/archive/detail/interface_iarchive.hpp:22,
from /usr/include/boost/archive/detail/common_iarchive.hpp:23,
from /usr/include/boost/archive/basic_text_iarchive.hpp:30,
from /usr/include/boost/archive/text_iarchive.hpp:24,
from face.h:9,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/boost/serialization/access.hpp:130:9: note: candidate expects 2 arguments, 0 provided
::new(t)T;
^~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/string:40,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/iomanip:40,
from face.h:2,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:341:12: note: candidate: ‘template<class _U1, class _U2, typename std::enable_if<(_MoveConstructiblePair<_U1, _U2>() && _ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&)’
constexpr pair(_U1&& __x, _U2&& __y)
^~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/string:40,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/iomanip:40,
from face.h:2,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:332:17: note: candidate: ‘template<class _U2, typename std::enable_if<_CopyMovePair<false, const long unsigned int, _U2>(), bool>::type <anonymous> > std::pair<_T1, _T2>::pair(const _T1&, _U2&&)’
explicit pair(const _T1& __x, _U2&& __y)
^~~~
/usr/include/c++/8/bits/stl_pair.h:332:17: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37,
from /usr/include/boost/archive/detail/iserializer.hpp:45,
from /usr/include/boost/archive/detail/interface_iarchive.hpp:22,
from /usr/include/boost/archive/detail/common_iarchive.hpp:23,
from /usr/include/boost/archive/basic_text_iarchive.hpp:30,
from /usr/include/boost/archive/text_iarchive.hpp:24,
from face.h:9,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/boost/serialization/access.hpp:130:9: note: candidate expects 2 arguments, 0 provided
::new(t)T;
^~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/string:40,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/iomanip:40,
from face.h:2,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:325:18: note: candidate: ‘template<class _U2, typename std::enable_if<_CopyMovePair<true, const long unsigned int, _U2>(), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(const _T1&, _U2&&)’
constexpr pair(const _T1& __x, _U2&& __y)
^~~~
/usr/include/c++/8/bits/stl_pair.h:325:18: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37,
from /usr/include/boost/archive/detail/iserializer.hpp:45,
from /usr/include/boost/archive/detail/interface_iarchive.hpp:22,
from /usr/include/boost/archive/detail/common_iarchive.hpp:23,
from /usr/include/boost/archive/basic_text_iarchive.hpp:30,
from /usr/include/boost/archive/text_iarchive.hpp:24,
from face.h:9,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/boost/serialization/access.hpp:130:9: note: candidate expects 2 arguments, 0 provided
::new(t)T;
^~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/string:40,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/iomanip:40,
from face.h:2,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:318:27: note: candidate: ‘template<class _U1, typename std::enable_if<_MoveCopyPair<false, _U1, Bend>(), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(_U1&&, const _T2&)’
explicit constexpr pair(_U1&& __x, const _T2& __y)
^~~~
/usr/include/c++/8/bits/stl_pair.h:318:27: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37,
from /usr/include/boost/archive/detail/iserializer.hpp:45,
from /usr/include/boost/archive/detail/interface_iarchive.hpp:22,
from /usr/include/boost/archive/detail/common_iarchive.hpp:23,
from /usr/include/boost/archive/basic_text_iarchive.hpp:30,
from /usr/include/boost/archive/text_iarchive.hpp:24,
from face.h:9,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/boost/serialization/access.hpp:130:9: note: candidate expects 2 arguments, 0 provided
::new(t)T;
^~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/string:40,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/iomanip:40,
from face.h:2,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:311:18: note: candidate: ‘template<class _U1, typename std::enable_if<_MoveCopyPair<true, _U1, Bend>(), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(_U1&&, const _T2&)’
constexpr pair(_U1&& __x, const _T2& __y)
^~~~
/usr/include/c++/8/bits/stl_pair.h:311:18: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37,
from /usr/include/boost/archive/detail/iserializer.hpp:45,
from /usr/include/boost/archive/detail/interface_iarchive.hpp:22,
from /usr/include/boost/archive/detail/common_iarchive.hpp:23,
from /usr/include/boost/archive/basic_text_iarchive.hpp:30,
from /usr/include/boost/archive/text_iarchive.hpp:24,
from face.h:9,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/boost/serialization/access.hpp:130:9: note: candidate expects 2 arguments, 0 provided
::new(t)T;
^~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/string:40,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/iomanip:40,
from face.h:2,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:304:17: note: candidate: ‘constexpr std::pair<_T1, _T2>::pair(std::pair<_T1, _T2>&&) [with _T1 = const long unsigned int; _T2 = Bend]’
constexpr pair(pair&&) = default;
^~~~
/usr/include/c++/8/bits/stl_pair.h:304:17: note: candidate expects 1 argument, 0 provided
/usr/include/c++/8/bits/stl_pair.h:303:17: note: candidate: ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const long unsigned int; _T2 = Bend]’
constexpr pair(const pair&) = default;
^~~~
/usr/include/c++/8/bits/stl_pair.h:303:17: note: candidate expects 1 argument, 0 provided
/usr/include/c++/8/bits/stl_pair.h:300:21: note: candidate: ‘template<class _U1, class _U2, typename std::enable_if<(std::_PCC<((! std::is_same<const long unsigned int, _U1>::value) || (! std::is_same<Bend, _U2>::value)), const long unsigned int, Bend>::_ConstructiblePair<_U1, _U2>() && (! std::_PCC<((! std::is_same<const long unsigned int, _U1>::value) || (! std::is_same<Bend, _U2>::value)), const long unsigned int, Bend>::_ImplicitlyConvertiblePair<_U1, _U2>())), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&)’
explicit constexpr pair(const pair<_U1, _U2>& __p)
^~~~
/usr/include/c++/8/bits/stl_pair.h:300:21: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37,
from /usr/include/boost/archive/detail/iserializer.hpp:45,
from /usr/include/boost/archive/detail/interface_iarchive.hpp:22,
from /usr/include/boost/archive/detail/common_iarchive.hpp:23,
from /usr/include/boost/archive/basic_text_iarchive.hpp:30,
from /usr/include/boost/archive/text_iarchive.hpp:24,
from face.h:9,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/boost/serialization/access.hpp:130:9: note: candidate expects 1 argument, 0 provided
::new(t)T;
^~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/string:40,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/iomanip:40,
from face.h:2,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:291:19: note: candidate: ‘template<class _U1, class _U2, typename std::enable_if<(std::_PCC<((! std::is_same<const long unsigned int, _U1>::value) || (! std::is_same<Bend, _U2>::value)), const long unsigned int, Bend>::_ConstructiblePair<_U1, _U2>() && std::_PCC<((! std::is_same<const long unsigned int, _U1>::value) || (! std::is_same<Bend, _U2>::value)), const long unsigned int, Bend>::_ImplicitlyConvertiblePair<_U1, _U2>()), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&)’
constexpr pair(const pair<_U1, _U2>& __p)
^~~~
/usr/include/c++/8/bits/stl_pair.h:291:19: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37,
from /usr/include/boost/archive/detail/iserializer.hpp:45,
from /usr/include/boost/archive/detail/interface_iarchive.hpp:22,
from /usr/include/boost/archive/detail/common_iarchive.hpp:23,
from /usr/include/boost/archive/basic_text_iarchive.hpp:30,
from /usr/include/boost/archive/text_iarchive.hpp:24,
from face.h:9,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/boost/serialization/access.hpp:130:9: note: candidate expects 1 argument, 0 provided
::new(t)T;
^~~~~~~~~
^~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/string:40,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/iomanip:40,
from face.h:2,
from bend.h:1,
from model.h:1,
from main.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:242:26: note: candidate: ‘template<class _U1, class _U2, typename std::enable_if<std::__and_<std::is_default_constructible<_Tp>, std::is_default_constructible<_Dp>, std::__not_<std::__and_<std::__is_implicitly_default_constructible<_U1>, std::__is_implicitly_default_constructible<_U2> > > >::value, bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair()’
explicit constexpr pair()
^~~~
/usr/include/c++/8/bits/stl_pair.h:242:26: note: template argument deduction/substitution failed:
/usr/include/c++/8/bits/stl_pair.h:241:59: error: no type named ‘type’ in ‘struct std::enable_if<false, bool>’
::value, bool>::type = false>
^~~~~
/usr/include/c++/8/bits/stl_pair.h:229:26: note: candidate: ‘template<class _U1, class _U2, typename std::enable_if<std::__and_<std::__is_implicitly_default_constructible<_U1>, std::__is_implicitly_default_constructible<_U2> >::value, bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair()’
_GLIBCXX_CONSTEXPR pair()
^~~~
/usr/include/c++/8/bits/stl_pair.h:229:26: note: template argument deduction/substitution failed:
/usr/include/c++/8/bits/stl_pair.h:227:59: error: no type named ‘type’ in ‘struct std::enable_if<false, bool>’
::value, bool>::type = true>
回答1:
You... never added serialization code. Look for the pattern:
class Face {
template <class Archive> void serialize(Archive& ar, unsigned) {
class Point {
template <class Archive> void serialize(Archive& ar, unsigned) {
class Bend : public Face {
template <class Archive> void serialize(Archive& ar, unsigned) {
class Model {
template <class Archive> void serialize(Archive& ar, Bend& bend, unsigned) {
You can probably spot what's wrong. Why is there a Bend&
in the argument list? It's not used. That should also tell you something.
Fixing:
template <class Archive>
void serialize(Archive& ar, unsigned) {
ar& bends;
}
Then it fails at:
static void construct(T * t){
// default is inplace invocation of default constructor
// Note the :: before the placement new. Required if the
// class doesn't have a class-specific placement new defined.
::new(t)T;
}
So you either need to load/save construct data OR you have to make a default constructor. I'll do the latter because it's simpler.
Bend(const size_t& faceID = 0, double angle = 0, size_t b_dir = 0, Point pnt = {}, Point dir = {});
This has a ripple effect:
Point(double X = 0, double Y = 0, double Z = 0) : X(X), Y(Y), Z(Z) {}
Now it compiles. The archive looks like:
22 serialization::archive 18 0 0 0 0 4 0 0 0 1 0 0 0 0 1 9.00000000000000000e+01 0 0 0 2.00000000000000000e+00 4.00000000000000000e+00 5.00000000000000000e+00 3.00000000000000000e+00 5.00000000000000000e+00 6.00000000000000000e+00 2 2 9.00000000000000000e+01 1 2.00000000000000000e+00 4.00000000000000000e+00 5.00000000000000000e+00 3.00000000000000000e+00 5.00000000000000000e+00 6.00000000000000000e+00 3 3 9.00000000000000000e+01 0 2.00000000000000000e+00 4.00000000000000000e+00 5.00000000000000000e+00 3.00000000000000000e+00 5.00000000000000000e+00 6.00000000000000000e+00 4 4 9.00000000000000000e+01 1 2.00000000000000000e+00 4.00000000000000000e+00 5.00000000000000000e+00 3.00000000000000000e+00 5.00000000000000000e+00 6.00000000000000000e+00
Roundtripping
A good sanity check is usually to roundtrip:
int main() {
{
Model test;
test.init();
save(test, "text_file.txt");
}
{
Model roundtrip;
restore(roundtrip, "text_file.txt");
save(roundtrip, "roundtrip.txt");
}
}
And indeed the files check out:
md5sum text_file.txt roundtrip.txt
13e472083bff9f51cf05a7c800d2e2bf text_file.txt
13e472083bff9f51cf05a7c800d2e2bf roundtrip.txt
Full Demo
Live On Coliru
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/map.hpp>
#include <iostream>
class Face {
friend std::ostream& operator<<(std::ostream& os, const Face& mf);
friend class boost::serialization::access;
template <class Archive> void serialize(Archive& ar, unsigned) {
ar & mFaceID;
}
public:
size_t mFaceID; /*!< an integer value represents the Face Identification
number */
void setFaceId(const size_t& faceID) { mFaceID = faceID; }
};
std::ostream& operator<<(std::ostream& os, const Face& mf) {
return os << ' ' << (size_t)mf.mFaceID << ' ';
}
class Point {
friend class boost::serialization::access;
friend std::ostream& operator<<(std::ostream& os, const Point& p);
template <class Archive> void serialize(Archive& ar, unsigned) {
// save/load base class information
ar & X & Y & Z;
}
public:
double X, Y, Z;
Point(double X = 0, double Y = 0, double Z = 0) : X(X), Y(Y), Z(Z) {}
};
class Bend : public Face {
friend class boost::serialization::access;
friend std::ostream& operator<<(std::ostream& os, const Bend& br);
template <class Archive> void serialize(Archive& ar, unsigned) {
// save/load base class information
ar & boost::serialization::base_object<Face>(*this);
ar & mBendAngle & mBendDirection;
ar & bendLinePnt & bendLineDir;
}
public:
double mBendAngle;
size_t mBendDirection;
Point bendLinePnt, bendLineDir;
Bend(const size_t& faceID = 0, double angle = 0, size_t b_dir = 0, Point pnt = {}, Point dir = {});
};
std::ostream& operator<<(std::ostream& os, const Bend& mb) {
os << mb.mBendAngle << " " << mb.mBendDirection << " " << mb.bendLinePnt.X
<< " " << mb.bendLinePnt.Y << " " << mb.bendLinePnt.Z;
os << mb.bendLineDir.X << " " << mb.bendLineDir.Y << " "
<< mb.bendLineDir.Z;
os << '\n';
return os;
}
Bend::Bend(const size_t& faceID, double angle, size_t b_dir, Point pnt, Point dir)
: mBendAngle(angle), mBendDirection(b_dir), bendLinePnt(pnt), bendLineDir(dir)
{
setFaceId(faceID);
}
class Model {
friend class boost::serialization::access;
friend std::ostream& operator<<(std::ostream& os, const Model& m);
template <class Archive>
void serialize(Archive& ar, unsigned) {
ar & bends;
}
public:
std::map<size_t, Bend> bends; // THIS IS THE MAP
void addBend(const Bend& bend);
void init();
};
std::ostream& operator<<(std::ostream& os, const Model& m) {
for (auto it = m.bends.begin(); it != m.bends.end(); it++) {
os << it->first << it->second;
}
return os;
}
void Model::addBend(const Bend& bend) { bends.insert({ bend.mFaceID, bend }); }
void Model::init() {
size_t faceID = 1;
size_t dir = 0;
Bend b1(faceID, 90, dir, Point(2, 4, 5), Point(3, 5, 6));
addBend(b1);
++faceID;
Bend b2(faceID, 90, dir + 1, Point(2, 4, 5), Point(3, 5, 6));
addBend(b2);
++faceID;
Bend b3(faceID, 90, dir, Point(2, 4, 5), Point(3, 5, 6));
addBend(b3);
++faceID;
Bend b4(faceID, 90, dir + 1, Point(2, 4, 5), Point(3, 5, 6));
addBend(b4);
}
//#include "model.h"
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <fstream>
void save(const Model& m, std::string filename) {
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << m;
}
void restore(Model& m, std::string filename) {
// open the archive
std::ifstream ifs(filename);
boost::archive::text_iarchive ia(ifs);
// restore the model from the archive
ia >> m;
}
int main() {
{
Model test;
test.init();
save(test, "text_file.txt");
}
{
Model roundtrip;
restore(roundtrip, "text_file.txt");
save(roundtrip, "roundtrip.txt");
}
}
来源:https://stackoverflow.com/questions/62524374/de-serializing-an-stl-map-class-member