我是C ++的新手,并且对我的程序有一些帮助来比较两个XML文件。这是我的代码:
#include "pugixml.hpp"
#include <iostream>
#include <unordered_map>
int main() {
pugi::xml_document doca, docb;
std::map<string, pugi::xml_node> mapa, mapb;
if (!doca.load_file("a.xml") || !docb.load_file("b.xml"))
return 1;
for (auto& node: doca.child("site_entries").children("entry")) {
const char* id = node.child_value("id");
mapa[new std::string(id, strlen(id))] = node;
}
for (auto& node: docb.child("site_entries").children("entry"))
const char* idcs = node.child_value("id");
std::string id = new std::string(idcs, strlen(idcs));
if (!mapa.erase(id)) {
mapb[id] = node;
}
}
}
当我尝试编译它时,我似乎遇到了很多错误。
我得到的第一个是:
src/main.cpp:10:14: error: unknown type name 'string'; did you mean 'std::string'?
std::map<string, pugi::xml_node> mapa, mapb;
~~~~~^~~
根据我的理解,我已正确指定。我应该按照要求改变它还是别的什么?
答案 0 :(得分:7)
您需要包含字符串库才能使用std::string
。
由于您提到很多错误,我怀疑您忘记包含<cstring>
以便使用strlen()
。
#include <string>
#include <cstring>
答案 1 :(得分:2)
您必须包含字符串库:
#include <string>
答案 2 :(得分:0)
使用以下方式:
std::string varName = "var value";
我正在使用Clion IDE,它对我有用。