可变范围 [英] Variable Scope

查看:62
本文介绍了可变范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码与GCC编译良好。这不应该是'/ i'重新定义''我'错误...


main()

{

for(int i = 0; i< 10; i ++)

cout<< i<< endl;


for(int i = 10; i> 0; i--)

cout<< i<<结束;

}


谢谢,

Prakash

The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of ''i'' error ...

main()
{
for (int i=0; i<10; i++)
cout << i << endl;

for (int i=10; i>0; i--)
cout << i << endl;
}

Thanks,
Prakash

推荐答案

MSVC ++编译器最终会出现重新定义错误。


1月24日下午3:17,PSN < prakash ... @ gmail.comwrote:
The MSVC++ compiler ends up with a redefinition error.

On Jan 24, 3:17 pm, "PSN" <prakash...@gmail.comwrote:

以下代码与GCC编译良好。这不应该是'/ i'重新定义''我'错误...


main()

{

for(int i = 0; i< 10; i ++)

cout<< i<< endl;


for(int i = 10; i> 0; i--)

cout<< i<<结束;


}谢谢,

Prakash
The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of ''i'' error ...

main()
{
for (int i=0; i<10; i++)
cout << i << endl;

for (int i=10; i>0; i--)
cout << i << endl;

}Thanks,
Prakash


PSN写道:
PSN wrote:

以下代码与GCC编译良好。这不应该是'/ i'重新定义''我'错误...


main()

{

for(int i = 0; i< 10; i ++)

cout<< i<< endl;


for(int i = 10; i> 0; i--)

cout<< i<< endl;

}
The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of ''i'' error ...

main()
{
for (int i=0; i<10; i++)
cout << i << endl;

for (int i=10; i>0; i--)
cout << i << endl;
}



否。这是''for'语句中声明的变量的规则。

在标准化之前,语言来回
a短暂几次,而一些编译器(VC ++)实现它

错误 ;方式,坚持下去。 VC ++ v6是预标准编译器

,也许你已经习惯了不正确的''for''变量规则...

现在是时候了_un-learn_坏习惯。 :-)


V

-

请在通过电子邮件回复时删除资金''A' br />
我没有回应最热门的回复,请不要问

No. That''s the rule for variables declared in the ''for'' statement.
Before it was standardized that way the language went back and forth
a couple of times briefly, and some compilers (VC++) implemented it
the "wrong" way, and held onto that. VC++ v6 is pre-standard compiler
and perhaps you''ve got used to the incorrect ''for'' variables rule...
It''s time to _un-learn_ bad habits. :-)

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




PSN napsal:

PSN napsal:

以下代码与GCC编译良好。这不应该是'/ i'重新定义''我'错误...


main()

{

for(int i = 0; i< 10; i ++)

cout<< i<< endl;


for(int i = 10; i> 0; i--)

cout<< i<<结束;

}


谢谢,

Prakash
The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of ''i'' error ...

main()
{
for (int i=0; i<10; i++)
cout << i << endl;

for (int i=10; i>0; i--)
cout << i << endl;
}

Thanks,
Prakash



AFAIK它在标准中被改变了(我不是多少年前),因此

''我''仅在循环内有效。在一些关于Mozilla的howto(如何编写

可移植代码)中提到过,有些编译器不会编译你的

示例。但我认为这是正确的标准(它不是重新定义)。

AFAIK it was changed (I do not how many years ago) in standard, so that
''i'' is valid only within loop. In some howto for Mozilla (how to write
portable code) was mentioned, that some compilers will not compile your
example. But I think it is correct acording to standard (it is not
redefinition).


这篇关于可变范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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