name lookup of `i' changed for new ISO `for' scoping

来源:互联网 发布:墙壁网络插座怎么接线 编辑:程序博客网 时间:2024/04/30 11:06

这个错误是不同编译器的苦恼!!

我将VC++可以编译通过的程序移植到LINUX使用g++编译产生的问题。

In file included from FDTD_2D_TE.cpp:7:
Matrix.h: In function `Tipus*** Init_Matrix_3D(int, int, int**)':
Matrix.h:195: name lookup of `i' changed for new ISO `for' scoping
Matrix.h:176:   using obsolete binding at `i'
make: *** [fdtd_2D_TE_PML.o] Error 1

它说的是 for 循环中在 “初始化”部分 定义的变量的作用域范围的一个问题。

ISO/ANSI C++ 把在此定义的变量的作用域范围限定在 for 循环体 内,或者说,出了循环体之外这个变量就无效了。

当前程序片断中的 变量i 即属于这种情况。

另一方面,不排除历史上 C++ 曾允许在循环体外使用这个变量。

在VC 6 中,i的作用域范围是函数作用域,在for循环外仍能使用变量i 即:
for (int i = 0; i < n; ++i) {
    //......
}
cout<<i<<endl;
可以通过

for (int i = 0; i < n; ++i) {
    //......
}
int i = 5;
则编译出错。

在DEV C++ 中,i的作用域仅限于for循环,即:
for (int i = 0; i < n; ++i) {
    //......
}
int i = 5;
可以通过

for (int i = 0; i < n; ++i) {
    //......
}
cout<<i<<endl;
则编译出错。

在vs.net 中,两种都能通过,但是若在for循环外使用i是,会给出警告。

更详细的分析见:http://hi.baidu.com/dwj192/blog/item/2015d9de0627bd5c94ee3791.html 

原创粉丝点击