错误消息:名称查找“jj”更改为 ISO“for”范围界定,(如果您使用“-fpermissive”G++ 将接受您的代码)
错误是:
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)
代码是:
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;
}
}
}
这里可能有什么问题?
编辑 1:
我将以下内容更改为:
for (int jj = 0; jj < routeVector[i].selectedBranchesVector.size(); jj++);
现在
int jj;
for (jj = 0; jj < routeVector[i].selectedBranchesVector.size(); jj++);
它可以工作了!!我不明白其中的原因。
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
内部 for 语句末尾有一个分号。这结束了
jj
的范围,因此它在块内不可见。编辑
您已经解决了范围问题,但仍然执行 for 循环,只需
删除括号后的分号即可!
You have a semicolon at the end of the inner for statement. That ends the scope of
jj
there, so it is not visible inside the block.Edit
You have solved the scope problem, but still have your for loop executing just
Remove the semicolon after the parenthesis!
该行以分号结束!不应该:)
This line ends with a semicolon! It shouldn't :)
某些编译器可能不接受在 for 循环结束后使用变量“jj”。由于该变量是在 for 循环内声明的,因此在执行后它会立即被销毁。因此,当您在 for 循环之外声明迭代变量时,它会保留在那里以供进一步使用。
实际上它并不是这样工作的,这就是为什么你可以通过添加“-fpermissive”来强制编译器忽略错误。
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.
In reality it doesn't work like that and that's why you can force the compiler to ignore the error by adding '-fpermissive'.
错误:
for(...;...;...;);
正确:
for(...;...;...;)
您不应使用分号,
;
False:
for(...;...;...;);
True:
for(...;...;...;)
You shouldn't use a semicolon,
;