gpt4 book ai didi

c++ - for循环中的逗号

转载 作者:太空狗 更新时间:2023-10-29 19:39:56 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com