反序列化 STL 映射 class 成员
De-Serializing an STL map class member
我是 boost & boost 序列化的新手。我正在尝试反序列化 STL 映射 class 成员。这是代码
:
...
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 << ' ';
}
点是下面定义的派生class的成员。
...
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)
{}
};
派生 class 面孔
...
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);
}
这个 class 包含我想要反序列化的地图:
...
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 用法:
#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;
}
序列化过程有效 好的,我需要有关 restore() 函数的帮助或改进我的代码。当我 运行 代码时,我得到一长串错误,例如:
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>
您...从未添加序列化代码。寻找模式:
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) {
您可能会发现问题所在。为什么参数列表中有一个Bend&
?它没有被使用。这也应该告诉你一些事情。
修复:
template <class Archive>
void serialize(Archive& ar, unsigned) {
ar& bends;
}
然后失败于:
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;
}
因此您要么需要 load/save construct data,要么必须创建默认构造函数。我会做后者,因为它更简单。
Bend(const size_t& faceID = 0, double angle = 0, size_t b_dir = 0, Point pnt = {}, Point dir = {});
这会产生连锁反应:
Point(double X = 0, double Y = 0, double Z = 0) : X(X), Y(Y), Z(Z) {}
现在可以编译了。存档看起来像:
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
往返
一个好的完整性检查通常是往返:
int main() {
{
Model test;
test.init();
save(test, "text_file.txt");
}
{
Model roundtrip;
restore(roundtrip, "text_file.txt");
save(roundtrip, "roundtrip.txt");
}
}
确实检查了文件:
md5sum text_file.txt roundtrip.txt
13e472083bff9f51cf05a7c800d2e2bf text_file.txt
13e472083bff9f51cf05a7c800d2e2bf roundtrip.txt
完整演示
#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");
}
}
我是 boost & boost 序列化的新手。我正在尝试反序列化 STL 映射 class 成员。这是代码 :
...
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 << ' ';
}
点是下面定义的派生class的成员。
...
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)
{}
};
派生 class 面孔
...
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);
}
这个 class 包含我想要反序列化的地图:
...
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 用法:
#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;
}
序列化过程有效 好的,我需要有关 restore() 函数的帮助或改进我的代码。当我 运行 代码时,我得到一长串错误,例如:
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>
您...从未添加序列化代码。寻找模式:
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) {
您可能会发现问题所在。为什么参数列表中有一个Bend&
?它没有被使用。这也应该告诉你一些事情。
修复:
template <class Archive>
void serialize(Archive& ar, unsigned) {
ar& bends;
}
然后失败于:
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;
}
因此您要么需要 load/save construct data,要么必须创建默认构造函数。我会做后者,因为它更简单。
Bend(const size_t& faceID = 0, double angle = 0, size_t b_dir = 0, Point pnt = {}, Point dir = {});
这会产生连锁反应:
Point(double X = 0, double Y = 0, double Z = 0) : X(X), Y(Y), Z(Z) {}
现在可以编译了。存档看起来像:
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
往返
一个好的完整性检查通常是往返:
int main() {
{
Model test;
test.init();
save(test, "text_file.txt");
}
{
Model roundtrip;
restore(roundtrip, "text_file.txt");
save(roundtrip, "roundtrip.txt");
}
}
确实检查了文件:
md5sum text_file.txt roundtrip.txt
13e472083bff9f51cf05a7c800d2e2bf text_file.txt
13e472083bff9f51cf05a7c800d2e2bf roundtrip.txt
完整演示
#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");
}
}