为什么在C ++中for循环后变量没有更新?

时间:2019-03-15 22:37:42

标签: c++

作为C ++的新手,为什么evaluate()在循环后不显示值?注意:我们正在遍历长度为5的自创建数组。

int i

这在控制台上返回:

int i = 0;
std::cout << i << "\n" // for testing: prints 0 

for (CArray<int>::Iter it(arr); it; ++it)
{
    if (*it != eq[i])
        return 1;
    ++i
    std::cout << i << "\n"; // for testing: prints 1, 2, 3, 4, 5
}

std::cout << i << "\n"; // why does this not print anything?

1 个答案:

答案 0 :(得分:2)

int i = 0;
std::cout << i << "\n" // for testing: prints 0 

for (CArray<int>::Iter it(arr); it; ++it)
{
    if (*it != eq[i]) {
        std::cout << "error" << "\n";
        return 1;
    }
    ++i
    std::cout << i << "\n"; // for testing: prints 1, 2, 3, 4, 5
}

std::cout << i << "\n"; // why does this not print anything?

这在控制台上返回:

0
1
2
3
4
5
error

所以,问题出在循环而不是变量。