没有可行的重载运算符用于引用映射

Jim*_*mmy 2 c++ xml xml-parsing c++11

我正在尝试使用地图,因此我可以将标签名称设为参考编号.当我尝试使用它时,就像在这段代码中我得到错误(每次我引用地图时总共6个):

src/main.cpp:25:45: error: no viable overloaded operator[] for type
      'std::map<std::string, std::string>'
                const char* idcs = node.child_value(tagMap[3]);
Run Code Online (Sandbox Code Playgroud)

这是代码:

#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;
    std::map<std::string, std::string> tagMap {{"1", "data"}, {"2", "entry"}, {"3", "id"}, {"4", "content"}};

    if (!doca.load_file("a.xml") || !docb.load_file("b.xml")) {
        std::cout << "Can't find input files";
        return 1;
    }

    for (auto& node: doca.child(tagMap[1]).children(tagMap[2])) {
        const char* id = node.child_value(tagMap[3]);
        mapa[id] = node;
    }

    for (auto& node: docb.child(tagMap[1]).children(tagMap[2])) {
        const char* idcs = node.child_value(tagMap[3]);
        if (!mapa.erase(idcs)) {
            mapb[idcs] = node;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Sar*_*ang 6

const char* idcs = node.child_value(tagMap[3]);
Run Code Online (Sandbox Code Playgroud)

是不正确的,tagMap只能通过keytype索引,这是std::string 你需要的是:

const std::string& idcs = node.child_value(tagMap["3"]);
Run Code Online (Sandbox Code Playgroud)