为什么我不应该把“using namespace std”放在在标题中?
有人曾经暗示不建议在头文件中这样做:
using namespace std;
Why is it not suggest?
它会导致像这样的链接器错误吗:(为了方便起见换行)
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)
Someone once hinted that doing this in a header file is not advised:
using namespace std;
Why is it not advised?
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为它强制任何使用您的头文件的人将 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 指令。当名称重叠时,这可能会导致混乱的错误。
If the file gets included elsewhere the compilation unit will implicitely get the using directive. This can lead to confusing errors when names overlap.