-1

I don't really have the hang of declaring identifiers. This is the code I have:

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

    if (!doca.load_file("a.xml") || !docb.load_file("b.xml"))
        return 1;

    for (auto& node: doca.child("site_entries").children("entry")) {
        const char* id = node.child_value("id");
        mapa[id] = node;
    }

    for (auto& node: docb.child("site_entries").children("entry"))
        const char* idcs = node.child_value("id");
        if (!mapa.erase(id)) {
            mapb[id] = node;
        }
    }

    for (auto& ea: mapa) {
        std::cout << "Removed:" << std::endl;
        ea.second.print(std::cout);
    }

    for (auto& eb: mapb) {
        std::cout << "Added:" << std::endl;
        eb.second.print(std::cout);
    }

I get quite a few errors when trying to compile but one that comes up a few times is the error error: use of undeclared identifier for example:

  • src/main.cpp:21:25: error: use of undeclared identifier 'id' if (!mapa.erase(id)) { ^
  • src/main.cpp:22:18: error: use of undeclared identifier 'id' mapb[id] = node; ^
  • src/main.cpp:22:24: error: use of undeclared identifier 'node' mapb[id] = node;

After reading this topic it seems to be talking about not including iostream without string, but I have done this.

https://stackoverflow.com/a/22197031/1738522

I'm very new to C++ so any help would be appreciated.

Community
  • 1
  • 1
J.Zil
  • 2,397
  • 7
  • 44
  • 78
  • 2
    Did you read the lines the error messages refer to? You declare a variable called `idcs` then refer to it as `id`. Change one name to match the other. – Mike Seymour Apr 17 '15 at 17:39
  • 1
    And it also seems like there's a missing opening brace in the second for loop. –  Apr 17 '15 at 17:39
  • Variables declared in one statement block (e.g. `for` loop), are not available to code outside of the statement block. Thus the variable `id` in the first `for` loop is not accessible by statements in the 2nd `for` loop. Maybe the error is a typo? – Thomas Matthews Apr 17 '15 at 20:02
  • @ThomasMatthews Does this mean the code as it is proposed here would not work since it can't compare? – J.Zil Apr 17 '15 at 20:20
  • It means that your code doesn't compile. – Thomas Matthews Apr 17 '15 at 22:21

4 Answers4

3

Well, you didn't declare id in that scope:

for (auto& node: docb.child("site_entries").children("entry")) { // brace added here.. dunno how this compiled
        const char* idcs = node.child_value("id");
        if (!mapa.erase(id)) {
            mapb[id] = node;
        }
    }

You use id, as declared in the previous for loop, instead of idcs.

2

You define id in this for block

for (auto& node: doca.child("site_entries").children("entry")) {
    const char* id = node.child_value("id");
    mapa[id] = node;
}

After this loop, id is destroyed ( it goes out of scope )

But you try to access it outside in

for (auto& node: docb.child("site_entries").children("entry"))
{  // you also forgot to put braces here
    const char* idcs = node.child_value("id");
    if (!mapa.erase(id)) {
        mapb[id] = node;
    }
}

I think you meant to use idcs instead of id

Arun A S
  • 6,421
  • 4
  • 29
  • 43
2
for (auto& node: doca.child("site_entries").children("entry")) {
    const char* id = node.child_value("id");
    mapa[id] = node;
}

id has the scope of the for loop. Then you do:

for (auto& node: docb.child("site_entries").children("entry")) { // you missed that curly bracket
    const char* idcs = node.child_value("id");
    if (!mapa.erase(id)) {
        mapb[id] = node;
    }
}

but id has gone out of scope. Probably you meant idcs and not id.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
2
#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;

    if (!doca.load_file("a.xml") || !docb.load_file("b.xml"))
        return 1;

    for (auto& node: doca.child("site_entries").children("entry")) {
        const char* id = node.child_value("id");
        mapa[id] = node;
    }

    for (auto& node: docb.child("site_entries").children("entry")) { // <-- here
        const char* idcs = node.child_value("id");
        if (!mapa.erase(idcs)) { // <-- here
            mapb[idcs] = node; // <-- here
        }
    }

    for (auto& ea: mapa) {
        std::cout << "Removed:" << std::endl;
        ea.second.print(std::cout);
    }

    for (auto& eb: mapb) {
        std::cout << "Added:" << std::endl;
        eb.second.print(std::cout);
    }

if you change it like this it should work without a problem.

You used id where you declared it as idcs and you were missing the opening brace of a for loop

Also note variables declared within the scope, meaning { here } become invalid once that scope is closed.

If you plan on using variables from outside the scope you need to declare them either at the beginning of the function or completely outside of any scope to make them global.

deW1
  • 5,562
  • 10
  • 38
  • 54