提问者:小点点

C++:对不存在的命名空间使用命名空间指令[重复]


我正在尝试用G++(7.5.0version)编译以下代码

using namespace nspace;
int main()
{
        return 0;
}

它给出的错误如下

$ g++ above_code.cpp 
namespaces_mystery1.cpp:1:17: error: ‘nspace’ is not a namespace-name
 using namespace nspace;
                 ^~~~~~
namespaces_mystery1.cpp:1:23: error: expected namespace-name before ‘;’ token
 using namespace nspace;
                       ^

以上行为正是我所期望的。

但是当我尝试编译下面的代码时,它编译得很好,没有像上面那样的错误。

using namespace std;
int main()
{
        return 0;
}

为什么名为std的命名空间与名为nspace的命名空间的行为不同


共1个答案

匿名用户

在遇到using命名空间nspace;时,不存在命名空间nspace,而遇到std命名空间。 后者可能是真的,因为隐式或显式包含了C++标准库的方面,或者编译器本身甚至可能对其进行硬编码。

如果你写了

namespace nspace{}

using语句之前,则编译将成功。

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|不存在|命名|空间|命名|空间|指令|重复)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?