c++ - No viable conversion from 'mapped_type' -


this code compare xml files, using map define xml tag names content.

#include "pugi/pugixml.hpp"  #include <iostream> #include <string> #include <map>  using namespace std;  int main() {     pugi::xml_document doca, docb;     std::map<std::string, pugi::xml_node> mapa, mapb;     std::map<int, std::string> tagmap {make_pair(1, "data"), make_pair(2, "entry"), make_pair(3, "id"), make_pair(4, "content")};      if (!doca.load_file("a.xml") || !docb.load_file("b.xml")) {         std::cout << "can't find input files";         return 1;     }      (auto& node: doca.child(tagmap[1]).children(tagmap[2])) {         auto id = node.child_value(tagmap[3]);         mapa[id] = node;     }      (auto& node: docb.child(tagmap[1]).children(tagmap[2])) {         auto idcs = node.child_value(tagmap[3]);         if (!mapa.erase(idcs)) {             mapb[idcs] = node;         }     } } 

the error this:

src/main.cpp:20:30: error: no viable conversion 'mapped_type' (aka 'std::__1::basic_string<char>')       'const char_t *' (aka 'const char *')         (auto& node: doca.child(tagmap[1]).children(tagmap[2])) {                                     ^~~~~~~~~ 

on recommendation tried approach:

        auto id = node.child_value(tagmap[3].c_str()); 

but still same error. seems me trying use map define tag names has caused lot of issues on hardcoding it, mapping seems logical since in future move map external file can run program different xml tags without recompiling each time.

if read error closely

src/main.cpp:20:30: error: no viable conversion 'mapped_type' (aka 'std::__1::basic_string<char>')   'const char_t *' (aka 'const char *')     (auto& node: doca.child(tagmap[1]).children(tagmap[2])) {                                 ^~~~~~~~~ 

you'll see you're passing std::string expects const char*. specifically:

doca.child(tagmap[1]) 

tagmap[1] std::string , child() expects const char*. so:

for (auto& node: doca.child(tagmap[1].c_str()).children(tagmap[2].c_str())) { 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -