不能在此范围内声明名为"Del"的局部变量,因为 [英] A local variable named 'Del' cannot be declared in this scope because

查看:438
本文介绍了不能在此范围内声明名为"Del"的局部变量,因为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不能在此范围内声明名为"Status"的局部变量,因为它将赋予"Status"不同的含义,"Status"已在父级或当前"范围中用于表示其他内容

A local variable named ''Status'' cannot be declared in this scope because it would give a different meaning to ''Status'' , which is already used in a ''parent or current'' scope to denote something else

推荐答案

bool status = true;
if (status == true)
{
    for (int status = 0; status < 1; status++)
    {
        //Do something
    }
}



在这种情况下,状态已在if块中声明,因此无法在子块(即For块)中声明.
每个变量都有一个与之关联的作用域,并且仅限于声明该变量的块.

例如



in this case status is already declared in the if block, so it can not be declared in the child block (i.e. For block).
Every variable has a scope associated with it, and it is limited to the block in which it is declared.

e.g.

static main()
{
    {
       int i = 0;
    }

    //i is not accessible here because it is declared in the different block.
}



这样,就不能在范围中声明两个具有相同名称的变量.如果这样做,则会出现一条错误消息:无法在此范围内声明名为"xyz"的局部变量,因为它会给"xyz"赋予不同的含义,而"xyz"已在"parent"中使用或当前的范围以表示其他内容"



With this, no two variables with the same name can be declared in the scope. If you do so, you get an error saying : "A local variable named ''xyz'' cannot be declared in this scope because it would give a different meaning to ''xyz'' , which is already used in a ''parent or current'' scope to denote something else"


只需重命名即可.注意可见性和范围.不知道那是什么吗? —只需重命名即可.

—SA
Just rename it. Mind visibility and scope. Not sure what is that? — just rename it.

—SA


这篇关于不能在此范围内声明名为"Del"的局部变量,因为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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