简体   繁体   English

c中的错误:声明在全局范围内隐藏了一个变量

[英]error in c: declaration shadows a variable in the global scope

When I try to compile the following code I get this error message: 当我尝试编译以下代码时,出现此错误消息:

error: declaration shadows a variable in the global scope: 错误:声明在全局范围内隐藏了一个变量:

void iterator(node* root) 无效迭代器(节点*根)

I don't understand where exactly I'm hiding or shadowing the global variable I've declared before. 我不知道我到底在哪里隐藏或遮盖了之前声明的全局变量。

How can I fix this? 我怎样才能解决这个问题?

// typedef node
typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;

node* root = NULL;

void iterator(node* root)
{
    for(int i = 0; i < 27; i++)
    {
        if (root -> children[i] != NULL)
        {
        iterator(root -> children[i]);
        }
    }
    free(root);
    return;
}

The compiler is sloppy in its error message; 编译器的错误消息很草率; “global scope” is not something defined in the C standard. 在C标准中没有定义“全局范围”。 What it is trying to tell you is: 它试图告诉您的是:

node* root = NULL;

declares root as an identifier at file scope (it is visible from its declaration through the end of the translation unit [the source file being compiled]), and: root声明为文件范围的标识符(从其声明到翻译单元(正在编译的源文件)末尾都可见),并且:

void iterator(node *root)

declares root as an identifier at block scope (it is visible from its declaration through the end of the block that defines the function). 在块范围内将root声明为标识符(从其声明到定义函数的块末尾都是可见的)。

These declarations refer to two different objects. 这些声明引用了两个不同的对象。 The first one is an object with static storage duration—it exists as long as your program is executing. 第一个是具有静态存储持续时间的对象-只要程序正在执行,它就一直存在。 The second one is a function parameter—it exists only while the function is executing, and there is a separate instance of it each time your function is called. 第二个参数是函数参数-仅在函数执行时存在,并且每次调用函数时都有一个单独的实例。

Inside the function, root refers only to the function parameter. 在函数内部, root仅指函数参数。 The former declaration is hidden and cannot be referred to by its name by any code inside the function. 前一个声明是隐藏的,并且函数内部的任何代码均不能使用其名称引用该声明。 (That is another bit of sloppiness in the compiler error message; the C standard uses “hide,” not “shadow.”) (这是编译器错误消息中的又一点草率; C标准使用“隐藏”,而不是“阴影”。)

There is nothing wrong with this in regard to the C standard—you are allowed to hide identifiers. 就C标准而言,这没有什么错-您可以隐藏标识符。 However, in regard to humans, it can cause problems because a person may write root in one place intended it to refer to the root in another place, because they did not see or forgot about the second declaration. 但是,对于人而言,这可能会引起问题,因为一个人可能会在一个地方写root ,以至于指另一地方的root ,因为他们没有看到或忘记了第二个声明。 This is why a compiler may have an optional warning about this. 这就是为什么编译器可能对此有可选警告的原因。 It appears you are compiling with that warning enabled, and with an option to elevate warnings into errors. 似乎您在启用警告的情况下进行编译,并且具有将警告提升为错误的选项。

To fix it, you should either use different names for the static object and the function parameter or should turn off the compiler warning for hiding identifiers, whichever you think suits your project. 要修复它,您应该为静态对象和函数参数使用不同的名称,或者关闭编译器警告以隐藏标识符,无论您认为哪种方式适合您的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM