mercredi 22 avril 2015

No viable conversion from 'mapped_type'

This is my code to compare XML files, using a map to define xml tag names to 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;
    }

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

    for (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 I get is this:

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

On recommendation I tried this approach:

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

But I still get same error. It seems me trying to use a map to define the tag names has caused a lot of issues over just hardcoding it, but mapping it seems logical since in the future I will move the map to an external file so I can run the program for different XML tags without recompiling each time.

Aucun commentaire:

Enregistrer un commentaire