使用 Boost 将结构序列化为 XML 时出错
Build error serializing struct to XML using Boost
我正在尝试使用 Boost 将一些结构序列化为 XML。我无法更改结构,所以我尝试以非侵入方式进行。
按照错误消息 docs I managed to get flat text serialization to work. However when I try to extend it to XML by looking at the XML example I'm unable to build and get errors from within the Boost libraries. I find only 中的简单“非侵入式版本”示例,我看不出如何将其应用于我的情况来解决它。一般寻找其他帖子和示例,我只看到不涉及将代码放入结构中的引用。
代码和错误如下。有没有对此足够熟悉的人指出我做错了什么?
密码是
#include <iostream>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <boost/config.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
void boostTest();
int main()
{
boostTest();
}
struct s_FileInfo
{
//const char* comment;
int test;
int motionCases; //number of motion cases
};
void boostTest()
{
struct s_FileInfo test = { 3,2 };
std::ofstream ofs("c:\sandpit\test.xml", std::ofstream::out);
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(test);
ofs.close();
}
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive& ar, s_FileInfo& g, const unsigned int version)
{
ar& g.test;
ar& g.motionCases;
}
} // namespace serialization
} // namespace
我在尝试构建时遇到的错误是
'mpl_assertion_in_line_6': const object must be initialized
'int boost::mpl::assertion_failed<false>(boost::mpl::assert<false>::type)': cannot convert argument 1 from 'boost::mpl::failed ************boost::serialization::is_wrapper<T>::* ***********' to 'boost::mpl::assert<false>::type'
两者都在 boost\archive\basic_xml_oarchive.hpp
断言说明了一切:
// 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 >));
那么,让我们补充一下
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <fstream>
void boostTest();
int main() { boostTest(); }
struct s_FileInfo {
// const char* comment;
int test;
int motionCases; // number of motion cases
};
void boostTest() {
s_FileInfo test {3, 2};
std::ofstream ofs("test.xml");
{
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(test);
}
}
namespace boost::serialization {
template <class Ar> void serialize(Ar& ar, s_FileInfo& g, unsigned /*unused*/) {
ar& BOOST_SERIALIZATION_NVP(g.test);
ar& BOOST_SERIALIZATION_NVP(g.motionCases);
}
} // namespace boost::serialization
创建 test.xml
:
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="19">
<test class_id="0" tracking_level="0" version="0">
<g.test>3</g.test>
<g.motionCases>2</g.motionCases>
</test>
</boost_serialization>
我正在尝试使用 Boost 将一些结构序列化为 XML。我无法更改结构,所以我尝试以非侵入方式进行。
按照错误消息 docs I managed to get flat text serialization to work. However when I try to extend it to XML by looking at the XML example I'm unable to build and get errors from within the Boost libraries. I find only
代码和错误如下。有没有对此足够熟悉的人指出我做错了什么?
密码是
#include <iostream>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <boost/config.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
void boostTest();
int main()
{
boostTest();
}
struct s_FileInfo
{
//const char* comment;
int test;
int motionCases; //number of motion cases
};
void boostTest()
{
struct s_FileInfo test = { 3,2 };
std::ofstream ofs("c:\sandpit\test.xml", std::ofstream::out);
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(test);
ofs.close();
}
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive& ar, s_FileInfo& g, const unsigned int version)
{
ar& g.test;
ar& g.motionCases;
}
} // namespace serialization
} // namespace
我在尝试构建时遇到的错误是
'mpl_assertion_in_line_6': const object must be initialized
'int boost::mpl::assertion_failed<false>(boost::mpl::assert<false>::type)': cannot convert argument 1 from 'boost::mpl::failed ************boost::serialization::is_wrapper<T>::* ***********' to 'boost::mpl::assert<false>::type'
两者都在 boost\archive\basic_xml_oarchive.hpp
断言说明了一切:
// 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 >));
那么,让我们补充一下
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <fstream>
void boostTest();
int main() { boostTest(); }
struct s_FileInfo {
// const char* comment;
int test;
int motionCases; // number of motion cases
};
void boostTest() {
s_FileInfo test {3, 2};
std::ofstream ofs("test.xml");
{
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(test);
}
}
namespace boost::serialization {
template <class Ar> void serialize(Ar& ar, s_FileInfo& g, unsigned /*unused*/) {
ar& BOOST_SERIALIZATION_NVP(g.test);
ar& BOOST_SERIALIZATION_NVP(g.motionCases);
}
} // namespace boost::serialization
创建 test.xml
:
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="19">
<test class_id="0" tracking_level="0" version="0">
<g.test>3</g.test>
<g.motionCases>2</g.motionCases>
</test>
</boost_serialization>