【问题标题】:No viable overloaded operator for references a map没有可行的重载运算符来引用地图
【发布时间】:2015-06-30 11:40:15
【问题描述】:

我正在尝试使用地图,以便将标签名称设置为参考号。当我尝试使用它时,就像在这段代码中一样,我得到了错误(每次我参考地图时总共有 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]);

这是代码:

#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;
        }
    }
}

【问题讨论】:

  • 您声明地图使用 字符串 作为键,而不是整数。
  • @JoachimPileborg 所以应该是这样的std::map&lt;int, std::string&gt; tagMap

标签: c++ xml c++11 xml-parsing


【解决方案1】:
const char* idcs = node.child_value(tagMap[3]);

不正确,tagMap 只能由键类型索引,即std::string 你需要的是:

const std::string& idcs = node.child_value(tagMap["3"]);

【讨论】:

  • 萨朗,快速提问。我也需要为这个做这个吗? const char* id = node.child_value(tagMap[3]);
  • 还有,@Sarang,我还需要更改我的地图以使用 int 吗?
  • 您将不得不更改 tagMap 索引器以使用字符串。 @更改地图以使用 int- 取决于您想要的方式
  • 但是如果我将 map 改为使用 int,我还需要按照您上面的建议进行操作吗?
  • 不,那不行,它只会像你之前所做的那样工作 - 只是 map 的类型将更改为 std::map
猜你喜欢
  • 2015-06-24
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多