2

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) 
1
  • I don't even put in my .cpp files. I prefer to use std::vector<std::string> etc
    – mike jones
    Commented Nov 11, 2011 at 22:38

2 Answers 2

10

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.

1

If the file gets included elsewhere the compilation unit will implicitely get the using directive. This can lead to confusing errors when names overlap.

2
  • could it cause linker errors like this: 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) ?? Commented Jul 6, 2010 at 12:49
  • @Tony: You should add this to the question. It would be easier to read with proper formatting.
    – pmr
    Commented Jul 6, 2010 at 12:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.