3

为什么以下行会产生错误?

for(int i = 0, int pos = 0, int next_pos = 0; i < 3; i++, pos = next_pos + 1) {
  // …
}

error: expected unqualified-id before ‘int’
error: ‘pos’ was not declared in this scope
error: ‘next_pos’ was not declared in this scope

编译器是 g++。

4

2 回答 2

15

每个语句只能有一种类型的声明,因此只需要一个 int:

for(int i = 0, pos = 0, next_pos = 0; i < 3; i++, pos = next_pos + 1)
于 2010-08-01T22:19:44.803 回答
4

在正常程序中:

int main()
{

int a=0,int b=0,int c=0;
return 0;    

}

永远不会工作,也不被接受。

这就是您在 for 循环中实际尝试做的事情!

于 2010-08-02T08:01:26.163 回答