0

我创建了一个小样本来测试 boost 序列化库,但是我遇到了编译问题。

首先,这是代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <boost/filesystem/operations.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/version.hpp>

std::vector<uint8_t> buf;

class MyClass
{
public:
    MyClass(){};
    virtual ~MyClass(){};

    int assetStatus;

    friend class boost::serialization::access;

    template<typename Archive> void serialize(
        Archive & ar,
        const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(assetStatus);
    }

    std::string ToString()
    {
        std::string toret;
        toret += " assetStatus: " + assetStatus;

        return toret;
    }
};

int main()
{
    MyClass a, b;
    a.assetStatus = 10;

    std::cout << a.ToString();

    boost::archive::xml_oarchive ooxml(std::ofstream(dbPath));
    ooxml << BOOST_SERIALIZATION_NVP(a); // error here

    MyClass d;
    boost::archive::xml_iarchive iixml(std::ifstream(dbPath));
    iixml >> BOOST_SERIALIZATION_NVP(d); // error here
    std::cout << d.ToString();
}

我在以下行收到编译错误:

ooxml << BOOST_SERIALIZATION_NVP(a);

iixml >> BOOST_SERIALIZATION_NVP(d);

错误是:

operator>>不匹配'iixml >> boost::serialization::make_nvp(const char*, T&) [with T=MyClass(((MyClass&)(&d)))]'

你对这个的含义有任何想法吗?

4

2 回答 2

1

看起来 dbPath 没有定义。此外,ooxml/iixml 的声明似乎不正确。

尝试修改您的代码以执行以下操作:...

const char * dbPath = "file.xml"

std::ofstream ofs(dbPath);
boost::archive::xml_oarchive ooxml(ofs);
ooxml << BOOST_SERIALIZATION_NVP(a); 

std::ifstream ifs(dbPath);
boost::archive::xml_iarchive iixml(ofs);
iixml >> BOOST_SERIALIZATION_NVP(d); 
于 2011-10-18T19:12:30.930 回答
0

我认为NVP(名称值对)不支持读取(即使用iixml),要么使用&(而不是>>)或iixml >> d;

于 2011-10-19T04:59:21.517 回答