C++ 使用boost::serialization将对象映射序列化为xml

C++ 使用boost::serialization将对象映射序列化为xml,c++,c++builder,boost-serialization,C++,C++builder,Boost Serialization,下面的序列化示例来自,这与我想做的几乎相同。但是,我已经更改了存档,以便将其序列化为XML。如果序列化为二进制文件,编译不会失败,但是序列化为xml时编译会失败。编译在basic\u xml\u oarchive.hpp中失败,方法如下: // boost code where compile fails template<class T> void save_override(T & t, BOOST_PFTO int) { // If your program f

下面的序列化示例来自,这与我想做的几乎相同。但是,我已经更改了存档,以便将其序列化为XML。如果序列化为二进制文件,编译不会失败,但是序列化为xml时编译会失败。编译在
basic\u xml\u oarchive.hpp中失败,方法如下:

// boost code where compile fails
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);
}
//编译失败的boost代码
模板
无效保存覆盖(T&T、增压到int)
{
//如果您的程序在这里编译失败,很可能是由于
//未在变量周围指定nvp包装,以
//将被序列化。
BOOST_MPL_ASSERT((serialization::is_wrapper));
此->详细信息\u公共\u oarchive::保存\u覆盖(t,0);
}
似乎我还没有做足够的工作来允许
std::map
对象被序列化,关于如何解决这个问题有什么想法吗


我的序列化实现:

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include <fstream>
#include <string>
#include <map>

using namespace std;

// This is a test class to use as the map data.
class CSomeData {
    public:
        CSomeData(){};
        CSomeData(float f0, string str0)
        {
            m_f0 = f0;
            m_str0 = str0;
        }

        float m_f0;
        string m_str0;

    private:
        friend class boost::serialization::access;

        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            ar & m_f0;
            ar & m_str0;
        }
};

// This is the class we really want to try serializing.
class CTest {
    public:
        CTest(){};
        CTest(int nNumber)
        {
            m_nNumber = nNumber;

            // Fill with some dummy data.
            m_mTst.insert(make_pair(0, CSomeData(0.23f, "hi hi hi")));
            m_mTst.insert(make_pair(1, CSomeData(7.65f, "second one")));
            m_mTst.insert(make_pair(2, CSomeData(9.23f, "third one")));
            m_mTst.insert(make_pair(3, CSomeData(5.6766, "chosen one")));
        }
        ~CTest(){};

        save()
        {
            std::ofstream ofs("filename");

            // Write class instance to archive. Writing seems to work ok.
            boost::archive::xml_oarchive oa(ofs);
            oa << BOOST_SERIALIZATION_NVP(*this);
        }

        int m_nNumber;

    private:
        map<int, CSomeData> m_mTst;

        friend class boost::serialization::access;

        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            ar & m_nNumber;
            ar & m_mTst;
        }
};
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//这是一个用作映射数据的测试类。
类CSomeData{
公众:
CSomeData(){};
CSomeData(浮点f0,字符串str0)
{
m_f0=f0;
m_str0=str0;
}
浮点数m_f0;
字符串m_str0;
私人:
好友类boost::serialization::access;
模板
无效序列化(存档和ar,常量未签名整数版本)
{
ar&m_f0;
ar&m_str0;
}
};
//这是我们真正想尝试序列化的类。
类别测试{
公众:
CTest(){};
CTest(国际编号)
{
m_nNumber=nNumber;
//填充一些虚拟数据。
m_mTst.insert(组成配对(0,CSomeData(0.23f,“hi-hi”));
m_mTst.insert(组成U对(1,CSomeData(7.65f,第二个));
m_mTst.insert(制造_对(2,CSomeData(9.23f,“第三个”));
m_mTst.insert(组成配对(3,CSomeData(5.6766,“选定的一个”));
}
~CTest(){};
保存()
{
std::ofs流(“文件名”);
//将类实例写入存档。写入似乎正常。
boost::archive::xml_oarchive oa(ofs);

oa我认为您需要为成员标记一个名称,以便进行XML序列化。这将指定要在XML中使用的元素名称。例如,使用以下内容:

ar & BOOST_SERIALIZATION_NVP(m_f0);
或者(在这种情况下更好):

二进制序列化将忽略标记。更多详细信息如下:


我想我的问题是codegear不支持序列化。这不起作用,我已经在使用
BOOST\u serialization\u NVP
宏,直到链接失败。我上面发布的codegear链接说不支持序列化。无论如何,谢谢你的帮助。
ar & make_nvp("field0", my_f0);