为什么我不应该将“using namespace std”放在标题中? [重复]

问题描述 投票:0回答:2

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

using namespace std;

为什么不建议这样做?

是否会导致像这样的链接器错误:(为了方便起见换行)

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) 
c++ header-files using
2个回答
10
投票

因为它强制任何使用你的头文件的人将

std
命名空间纳入全局范围。如果他们有一个与标准库类之一同名的类,这可能会成为问题。


1
投票

如果文件被包含在其他地方,编译单元将隐式获取 using 指令。当名称重叠时,这可能会导致混乱的错误。

© www.soinside.com 2019 - 2024. All rights reserved.