我正在尝试从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
}
答案 0 :(得分:3)
从您的代码和您正在编写的内容中,您将尝试阅读。在这种情况下,您不应使用ofstream和xml_oarchive,而应使用 ifstream 和 xml_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);