简单C ++程序上的多个未声明标识符

时间:2015-04-17 17:38:02

标签: c++ c++11

我真的不知道如何声明标识符。这是我的代码:

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

我在尝试编译时遇到了一些错误但是出现了几次错误的错误是error: use of undeclared identifier,例如:

  • src / main.cpp:21:25:错误:使用未声明的标识符'id'     if(!mapa.erase(id)){                     ^
  • src / main.cpp:22:18:错误:使用未声明的标识符'id'         mapb [id] = node;              ^
  • src / main.cpp:22:24:错误:使用未声明的标识符'node'         mapb [id] = node;

在阅读了这个主题后,它似乎在谈论不包括没有字符串的iostream,但我已经这样做了。

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

我对C ++很陌生,所以任何帮助都会受到赞赏。

4 个答案:

答案 0 :(得分:3)

嗯,您没有在该范围内声明id

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

您使用前一个for循环中声明的id,而不是idcs

答案 1 :(得分:2)

您可以在此块中为{<1}}定义

id

在此循环之后,for (auto& node: doca.child("site_entries").children("entry")) { const char* id = node.child_value("id"); mapa[id] = node; } 被销毁(它超出范围)

但是你试图在

外面访问它
id

我认为您打算使用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; } } 代替idcs

答案 2 :(得分:2)

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

id具有for循环的范围。然后你做:

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

id已超出范围。可能你的意思是idcs而不是id

答案 3 :(得分: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);
    }

如果你这样改变它应该没有问题。

您使用了id,并将其声明为idcs,而您缺少for循环的左括号

另请注意在范围内声明的变量,意味着{ here }在范围关闭后变为无效。

如果您打算在范围外使用变量,则需要在函数的开头声明它们,或者完全在任何范围之外声明它们以使它们成为全局变量。