有人曾经暗示不建议在头文件中这样做:
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)
因为它强制任何使用你的头文件的人将
std
命名空间纳入全局范围。如果他们有一个与标准库类之一同名的类,这可能会成为问题。
如果文件被包含在其他地方,编译单元将隐式获取 using 指令。当名称重叠时,这可能会导致混乱的错误。