为什么我不应该把“using namespace std”在标题? [英] Why shouldn't I put "using namespace std" in a header?

查看:163
本文介绍了为什么我不应该把“using namespace std”在标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人曾经暗示过不要在头文件中这样做:

Someone once hinted that doing this in a header file is not advised:

using namespace std;

为什么不建议?

它会导致链接器错误,如下所示:(为了方便起见,为了方便起见)

Could it cause linker errors like this: (linewrapped for convenience)

error LNK2005: "public: __thiscall std::basic_string<char,struct 
std::char_traits<char>,class std::allocator<char> >::
~basic_string<char,struct std::char_traits<char>,class std::allocator<char> > 
(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) 
already defined in tools.lib(Exception.obj) 


推荐答案

因为它强制任何使用头文件的人将 std 命名空间带到全局范围。这可能是一个问题,如果他们有一个类与一个标准库类相同的名称。

Because it forces anyone who uses your header file to bring the std namespace into global scope. This could be a problem if they have a class that has the same name as one of the standard library classes.

这篇关于为什么我不应该把“using namespace std”在标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆