I am trying to learn how to use boost::serialization for recording an object. For example, I am using the following codes to serialize an object:
struct Abc
{
int a;
float b;
double c;
};
namespace boost
{
namespace serialization
{
template <typename Archive>
void serialize(Archive& ar, Abc &obj,const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(obj.a);
ar & BOOST_SERIALIZATION_NVP(obj.b);
ar & BOOST_SERIALIZATION_NVP(obj.c);
};
}
};
Abc kkk;
std::ofstream ofs(tmpFile);
boost::archive::xml_oarchive ar(ofs);
ar & kkk;
ofs.close();
However, when I compile the codes, I have the following compilation error:
Error 1 error C2664: 'boost::mpl::assertion_failed' : cannot convert parameter 1 from 'boost::mpl::failed ************boost::serialization::is_wrapper<T>::* ***********' to 'boost::mpl::assert<false>::type' \boost_1_50_0\boost\archive\basic_xml_oarchive.hpp 92
The error message leads to the source code of boost:
template<class T>
void save_override(T & t, BOOST_PFTO int)
{
// If your program fails to compile here, its most likely due to
// not specifying an nvp wrapper around the variable to
// be serialized.
BOOST_MPL_ASSERT((serialization::is_wrapper< T >));
this->detail_common_oarchive::save_override(t, 0);
}
Any ideas? Thanks.
You need to make your myAbc
instance of Abc
in your code at the bottom an NVP, too:
ar & BOOST_SERIALIZATION_NVP(myAbc);
Everything that's serialized to an XML archive must be constructed this way, not just the subelements.