0

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.

5
  • 1
    Please see the line where it shows the error.... and where you're actually changing your code. Apr 22, 2015 at 18:52
  • Just a guess: const auto&
    – user2249683
    Apr 22, 2015 at 18:54
  • @DieterLücking const auto id = node.child_value(tagMap[3].c_str()); Like this? I get the same error
    – J.Zil
    Apr 22, 2015 at 18:55
  • @Nawaz I see the line with the error but I don't understand it.
    – J.Zil
    Apr 22, 2015 at 18:57
  • @JamesWillson +& (in your original code)
    – user2249683
    Apr 22, 2015 at 18:58

1 Answer 1

2

If you read the error closely

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])) {
                                ^~~~~~~~~

You'll see that you're passing a std::string to something that expects a const char*. Specifically:

doca.child(tagMap[1])

tagMap[1] is a std::string and child() expects a const char*. So:

for (auto& node: doca.child(tagMap[1].c_str()).children(tagMap[2].c_str())) {
4
  • Thank you very much. I tried this code, but it gives me this error: src/main.cpp:21:9: error: non-const lvalue reference to type 'const char *' cannot bind to a temporary of type 'const char_t *' (aka 'const char *') auto& id = node.child_value(tagMap[3].c_str()); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Does tis mean it should be for (const &auto...
    – J.Zil
    Apr 22, 2015 at 19:02
  • @JamesWillson The error states that you're binding a non-const reference to a temporary. What does that suggest to you that the fix is?
    – Barry
    Apr 22, 2015 at 19:04
  • I'll try a few things and get back to you
    – J.Zil
    Apr 22, 2015 at 19:05
  • Putting const here for (const auto& node.... and here auto id = node... worked! Thank you
    – J.Zil
    Apr 22, 2015 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.