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

查看:787
本文介绍了c中的错误:声明在全局范围内隐藏了一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译以下代码时,出现此错误消息:

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.

我该如何解决?

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

推荐答案

编译器的错误消息很草率; 全局范围"不是C标准中定义的内容.它试图告诉您的是:

The compiler is sloppy in its error message; "global scope" is not something defined in the C standard. What it is trying to tell you is:

node* root = NULL;

root声明为文件范围的标识符(从其声明到翻译单元(正在编译的源文件)末尾都是可见的),并且:

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:

void iterator(node *root)

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).

这些声明引用了两个不同的对象.第一个是具有静态存储持续时间的对象-只要程序正在执行,它就一直存在.第二个是函数参数-仅在函数执行时存在,并且每次调用函数时都有一个单独的实例.

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.

在函数内部,root仅引用函数参数.前一个声明是隐藏的,并且函数内部的任何代码均不能使用其名称引用该声明. (这是编译器错误消息中的另一点草率; C标准使用隐藏",而不是阴影".)

Inside the function, root refers only to the function parameter. 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标准,这没有错-允许您隐藏标识符.但是,对于人而言,这可能会引起问题,因为一个人可能会在一个地方写root,而在另一个地方却要引用root,因为他们没有看到或忘记了第二个声明.这就是为什么编译器可能对此有可选警告的原因.看来您正在编译时启用了该警告,并带有一个将警告提升为错误的选项.

There is nothing wrong with this in regard to the C standard—you are allowed to hide identifiers. 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. 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.

要解决此问题,您应该为静态对象和function参数使用不同的名称,或者关闭编译器警告以隐藏标识符,无论您认为哪种方式适合您的项目.

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.

这篇关于c中的错误:声明在全局范围内隐藏了一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆