for循环中的逗号

发布于 2024-09-12 18:10:18 字数 293 浏览 2 评论 0原文

为什么下面的行会产生错误?

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++。

Why is the following line producing errors?

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

Compiler is g++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-09-19 18:10:18

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

for(int i = 0, pos = 0, next_pos = 0; i < 3; i++, pos = next_pos + 1)

You can have only one type of declaration per statement, so you only need one int:

for(int i = 0, pos = 0, next_pos = 0; i < 3; i++, pos = next_pos + 1)
慢慢从新开始 2024-09-19 18:10:18

在正常程序中:

int main()
{

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

}

永远不会工作并且不被接受。

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

In a normal program:

int main()
{

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

}

will never work and is not accepted.

This is what you are actually trying to do inside the for loop!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文