C++ 错误消息:'jj' 的名称查找已更改为 ISO 'for' 范围,(如果您使用 '-fpermissive' G++ 将接受您的代码)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6556449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:21:04  来源:igfitidea点击:

Error message: name lookup of ‘jj’ changed for ISO ‘for’ scoping, (if you use ‘-fpermissive’ G++ will accept your code)

c++compiler-errors

提问by Aquarius_Girl

The error is:

错误是:

In function ‘int returnShortestWeightedBranch(std::vector<route, std::allocator<route> >*)':
error: name lookup of ‘jj' changed for ISO ‘for' scoping
note: (if you use ‘-fpermissive' G++ will accept your code)

The code is:

代码是:

for (int i = 0; i< routeVector.size(); i++)
        {
            if (routeVector[i].exitPoint == exitPointDetailsVector[preserveL].exitPoint)
            {
                cout << "\n-----------parent: " << routeVector[i].exitPoint;
                branch obj;
                obj.connectedExitPoint = exitPointDetailsVector[preserveI].exitPoint;
                routeVector[i].selectedBranchesVector.push_back (obj);

                for (int jj = 0; jj < routeVector[i].selectedBranchesVector.size(); jj++);
                {
                    cout << "\n-----------branch: " << routeVector[i].selectedBranchesVector[jj].connectedExitPoint;
                }
            }
        }

What can be the problem here?

这里可能有什么问题?

EDIT 1:

编辑 1:

I changed the following:

我更改了以下内容:

for (int jj = 0; jj < routeVector[i].selectedBranchesVector.size(); jj++);

to:

到:

int jj;
for (jj = 0; jj < routeVector[i].selectedBranchesVector.size(); jj++);

and now it is working!! I fail to understand the REASONS.

现在它正在工作!我不明白原因。

回答by Bo Persson

You have a semicolon at the end of the inner for statement. That ends the scope of jjthere, so it is not visible inside the block.

内部 for 语句的末尾有一个分号。这结束了jj那里的范围,所以它在块内不可见。

Edit
You have solved the scope problem, but still have your for loop executing just

编辑
您已经解决了范围问题,但您的 for 循环仍然只执行

<nothing>;

Remove the semicolon after the parenthesis!

去掉括号后的分号!

回答by Kornel Kisielewicz

for (int jj = 0; jj < routeVector[i].selectedBranchesVector.size(); jj++);

This line ends with a semicolon! It shouldn't :)

这一行以分号结束!它不应该:)

回答by Matheus Z

Some compilers may not accept the use of the variable 'jj' after the for loop is ended. Since the variable is declared inside the for loop, it is destroyed right after it is executed. Thus when you declare your iteration variable outside the for loop it remains there for further use.

在 for 循环结束后,某些编译器可能不接受使用变量“jj”。由于变量是在 for 循环中声明的,所以它在执行后立即被销毁。因此,当您在 for 循环之外声明迭代变量时,它会保留在那里以供进一步使用。

In reality it doesn't work like that and that's why you can force the compiler to ignore the error by adding '-fpermissive'.

实际上它不是那样工作的,这就是为什么您可以通过添加“-fpermissive”来强制编译器忽略错误。

回答by Emre q

False: for(...;...;...;);

错误的: for(...;...;...;);

True: for(...;...;...;)

真的: for(...;...;...;)

You shouldn't use a semicolon, ;

你不应该使用分号, ;