【发布时间】:2021-04-19 11:33:42
【问题描述】:
我正在尝试使用以下代码中的 using 指令访问变量 x:
#include <iostream>
using namespace std;
int x = 10;
namespace e {
int x = 5;
}
int main() {
using namespace e; // Because of this line compiler shows error
cout << x;
return 0;
}
通常我们使用以下行来访问 x 但我收到错误 我们也可以使用 using e::x; 但我的问题是为什么我们不能使用 using namespace e;
【问题讨论】:
-
注意,错误不在你指出的那一行,而是在下面。如果你有
using namespace e;,编译器如何区分::x和::e::x? -
我是第一次学习这个主题,你能解释清楚吗?
-
这能回答你的问题吗? Why and how should I use namespaces in C++?
-
我不这么认为
标签: c++ namespaces using-directives