问题描述
for (int i = 0; i <10; i++){富();}诠释 i = 10;//错误,'i' 已经存在--------------------------------------for (int i = 0; i <10; i++){富();}我 = 10;//错误,'i' 不存在
根据我对范围的理解,第一个示例应该没问题.他们都不被允许的事实似乎更加奇怪.当然,我"要么在范围内,要么不在范围内.
我不明白范围是否有一些不明显的地方,这意味着编译器真的无法解决这个问题?或者只是保姆国家编译主义的一个例子?
根据我对范围的理解,第一个示例应该没问题.
您对范围的理解很好.这不是范围界定错误.这是简单名称使用不一致的错误.
<块引用>int i = 10;//错误,'i' 已经存在
这不是报告的错误.报告的错误是不能在此范围内声明名为 i 的局部变量 因为它会给已在子范围内用于表示其他内容的 i 赋予不同的含义"
错误信息告诉你错误是什么;再次阅读错误信息.没有任何地方说声明之间存在冲突;它说错误是因为它改变了简单名称的含义.错误是不是重新声明;在两个不同的范围内拥有相同名称的两个东西是完全合法的,即使这些范围嵌套.不合法的是,一个简单的名字在嵌套的局部变量声明空间中意味着两个不同的东西.
如果您执行类似的操作,则会收到错误名为 i 的局部变量已在此范围内定义"
int i = 10;诠释 i = 10;
<块引用>
当然,i"要么在范围内,要么不在范围内.
当然——但那又怎样?给定的 i 是否在范围内是无关紧要的.例如:
C 类{诠释我;无效 M(){字符串 i;
完全合法.外部 i 在整个 M 的范围内.声明一个遮蔽外部范围的本地 i 完全没有问题.如果你说会有什么问题
C 类{诠释我;无效 M(){诠释 x = 我;foreach(char i in ...
因为现在您在两个嵌套的局部变量声明空间中使用 i 来表示两个不同的东西——一个循环变量和一个字段.这令人困惑且容易出错,因此我们将其设为非法.
<块引用>我不明白范围是否有一些不明显的地方,这意味着编译器真的无法解决这个问题?
我不明白这个问题.显然编译器能够完整地分析程序;如果编译器无法解析 i 的每个用法的含义,那么它如何报告错误消息? 编译器完全能够确定您使用 'i' 来表示两个不同的东西相同的局部变量声明空间,并相应地报告错误.
for (int i = 0; i < 10; i++)
{
Foo();
}
int i = 10; // error, 'i' already exists
----------------------------------------
for (int i = 0; i < 10; i++)
{
Foo();
}
i = 10; // error, 'i' doesn't exist
By my understanding of scope, the first example should be fine. The fact neither of them are allowed seems even more odd. Surely 'i' is either in scope or not.
Is there something non-obvious about scope I don't understand which means the compiler genuinely can't resolve this? Or is just a case of nanny-state compilerism?
By my understanding of scope, the first example should be fine.
Your understanding of scope is fine. This is not a scoping error. It is an inconsistent use of simple name error.
int i = 10; // error, 'i' already exists
That is not the error that is reported. The error that is reported is "a local variable named i cannot be declared in this scope because it would give a different meaning to i which is already used in a child scope to denote something else"
The error message is telling you what the error is; read the error message again. It nowhere says that there is a conflict between the declarations; it says that the error is because that changes the meaning of the simple name. The error is not the redeclaration; it is perfectly legal to have two things in two different scopes that have the same name, even if those scopes nest. What is not legal is to have one simple name mean two different things in nested local variable declarations spaces.
You would get the error "a local variable named i is already defined in this scope" if instead you did something like
int i = 10;
int i = 10;
Surely 'i' is either in scope or not.
Sure -- but so what? Whether a given i is in scope or not is irrelevant. For example:
class C
{
int i;
void M()
{
string i;
Perfectly legal. The outer i is in scope throughout M. There is no problem at all with declaring a local i that shadows the outer scope. What would be a problem is if you said
class C
{
int i;
void M()
{
int x = i;
foreach(char i in ...
Because now you've used i to mean two different things in two nested local variable declaration spaces -- a loop variable and a field. That's confusing and error-prone, so we make it illegal.
Is there something non-obvious about scope I don't understand which means the compiler genuinely can't resolve this?
I don't understand the question. Obviously the compiler is able to completely analyze the program; if the compiler could not resolve the meaning of each usage of i then how could it report the error message? The compiler is completely able to determine that you've used 'i' to mean two different things in the same local variable declaration space, and reports the error accordingly.
这篇关于语句块中的变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!