【发布时间】:2015-06-24 16:08:00
【问题描述】:
这是我的一些代码:
#include "pugi/pugixml.hpp"
#include <iostream>
#include <string>
#include <map>
int main() {
pugi::xml_document doca, docb;
std::map<std::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:16:13: error: no viable overloaded operator[] for type 'std::map<std::string, pugi::xml_node>'
mapa[new std::string(id, strlen(id))] = node;
【问题讨论】:
-
new std::string...-->std::string -
你是java人吗?因为我在这里感觉Java:
std::string id = new std::string(idcs, strlen(idcs));。你不要newC++ 中的局部变量。 -
现在,你不会在 C++ 中
new任何东西。