2

我正在尝试从 XML 文件加载一个简单的类,但出现以下错误:

error: no match for 'operator>>' in 'xml >> boost::serialization::make_nvp(const
char*, T&) [with T = Options](((Options&)(& o)))'

我做错了一定是愚蠢的,但我不知道是什么。有人有想法吗?这是我的代码:

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

class Options {
public:
    Options() {
        SetInteger("screenWidth", 1024);
        SetInteger("screenHeight", 768);
    }
    void SetInteger(const std::string& name, int value) {
        integers_[name] = value;
    }
private:
    std::map<std::string, int> integers_;

    friend class boost::serialization::access;
    template<class archive>
    void serialize(archive& ar, const unsigned int version)
    {
        using boost::serialization::make_nvp;
        ar & make_nvp("integers", integers_);
    }
};

int main() {
    Options o;
    std::ofstream ifs("input.xml");
    boost::archive::xml_oarchive xml(ifs);
    xml >> boost::serialization::make_nvp("options", o); // error
}
4

1 回答 1

4

好吧,从您的代码和您正在编写的内容来看,您正在尝试阅读。在这种情况下,您不应该使用 ofstream 和 xml_oarchive,而是使用ifstreamxml_iarchive

#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>

....

//for read
std::ifstream ifs("input.xml");
boost::archive::xml_iarchive xmlIn(ifs);
xmlIn >> boost::serialization::make_nvp("options", o); 

//for write
std::ofstream ofs("output.xml");
boost::archive::xml_oarchive xmlOut(ofs);
xmlOut << boost::serialization::make_nvp("options", o); 
于 2011-04-20T12:25:35.130 回答