c++ - for循环中的逗号

标签 c++ for-loop comma-operator

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

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

最佳答案

每个语句只能有一种类型的声明,所以你只需要一个 int:

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

关于c++ - for循环中的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3383968/

相关文章:

c++ - ReadProcessMemory 的缓冲区应该是什么数据类型?

c++ - C++ 中逗号运算符的语法使用

c++ - 无法编译摩西(对 boost::... 的 undefined reference )

c++ - std::rank 的目的是什么?

javascript - For 循环图片库

php - 当 num_rows 返回 1 时,For 循环不执行

c - 请解释此程序中的逗号运算符

C++ 逗号运算符重载和引用 vector

c++ - 如果在某些输入之后使用 getline() 将不起作用

c - C 实现中的 For 循环