【发布时间】:2021-04-25 04:00:04
【问题描述】:
下面是代码
#include <iostream>
using namespace std;
int main()
{
int x=5, y=6;
int out = x + y;
{
int out= 89;
cout << :: out << "\n";
}
cout << out;
}
为此我得到了
error: ‘::out’ has not been declared
12 | cout << :: out << "\n";
| ^~~
编辑:我期待它打印值为 11 的变量 out(如 python 中的 nonlocal),但我得到了错误。我该如何解决这个问题?
【问题讨论】:
-
在显示的代码中没有名为
out的全局变量。有两个局部变量都命名为out,两者都不能称为::out。您可以通过为不同的变量赋予不同的名称并删除::来解决此问题 -
@IgorTandetnik,@songyanyao。感谢您指出。但是,如果我想访问外部块的
out而不是内部块怎么办(例如 python 语言中的nonlocal)。 -
没有语法。只是给他们不同的名字。
-
这里没有错误消息指出“范围解析运算符尚未声明”。您必须准确引用错误消息,这也意味着您必须准确阅读它们。这也有助于防止您草率下结论。
-
@AdityaSinghRathore。这真的很有帮助。
标签: c++