# using std::*** problem

This topic is 4963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

just 2 questions: 1. Why it is not recommended to use 'using' directive in header files? 2. I have sth like this in class header: map<string, CDataAtom>::iterator commonIter; and i don't want to write 'using namespace std;' before my class in header file. I want to explicity declare what i'm interested in, for example: using std::map; using std::string; etc. but i don't know what i should add for this above iterator. Still have this error (in MSVC6): error C2653: 'map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class CDataAtom,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<c har> > >,class std::allocator<class CDataAtom> >' : is not a class or namespace name so i'm forced to add 'std::' prefix to iterator's declaration. But i don't like if there is sth that i think i should know but i don't ;) any ideas? thanks

##### Share on other sites
Quote:
 Original post by TomMarc1. Why it is not recommended to use 'using' directive in header files?
Because it pollutes the global namespace of files that include it, which can cause collisions.

In your case, put your stuff inside a namespace and pull in the declarations you want inside that namespace. Personally, I don't see the big deal about five extra characters, but YMMV.

namespace TomMarc{  using std::map;    // code};

##### Share on other sites
Quote:
 Original post by TomMarcjust 2 questions:1. Why it is not recommended to use 'using' directive in header files?

'using' is usually okay. What's not usually recommended is 'using namespace' because it causes namespace pollution.
Quote:
 2. I have sth like this in class header:map::iterator commonIter;and i don't want to write 'using namespace std;' before my class in header file. I want to explicity declare what i'm interested in, for example:using std::map;using std::string;etc.but i don't know what i should add for this above iterator. Still have this error (in MSVC6):error C2653: 'map,class std::allocator >,class CDataAtom,struct std::less,class std::allocator > >,class std::allocator >' : is not a class or namespace name

Either it's an error in your compiler or library, or there's some other error in your code that I don't know about. It works fine for me. Version 6 is notoriously unconforming and does its own thing with templates.

##### Share on other sites
You don't put using directives in header files because it can subtly change the meaning of code which includes that header file due to name resolution. The current recommendation from the chairman of the C++ standards committee is that using directives should be omitted from all header files, and in .cpp files should be listed after all includes.

I've learnt my lesson about how hard this bites - I spent most of today factoring code because a third party library and its use of using directives and poor planning with regard to naming collisions.

The easiest way to avoid the extra typing problems is to use typedefs in appropriate places. Be aware that using std::xxx in header files and a using directive in the .cpp file will confuse vc6's intellisense, as will typedefs in some cases.

##### Share on other sites
Quote:
 Original post by XXX_Andrew_XXXI've learnt my lesson about how hard this bites - I spent most of today factoring code because a third party library and its use of using directives and poor planning with regard to naming collisions.

Can you say Borland's VCL? Say thankya!

##### Share on other sites
Thanks for your replies, i understand now why it is not recommended to use 'using' in headers. But this second question again. Let's say i have this code in cpp file:
// FILE START
#pragma warning(disable: 4786)

#include <map>

using std::map;
using std::??? <-- what i should type in here to compile? :)

class Bar
{
private:
map<int, int>::iterator commonIter;
};

int main() { return 0; }
// FILE END
I know that i can just type 'using namespace std;', but just want to know how i can do it with declaration like above.
thx

##### Share on other sites
The above code is fine...what are you trying to do? If you just don't want to have to type the "map<int, int>" for everything, you can typedef it. 'using' basically just makes it so you don't need to include namespace and/or scope/class qualifiers.

typedef map<int, int>::iterator mapIterator;

If I misunderstood your question, ignore me :P

##### Share on other sites
ok. i just compiled it under gcc.. strange thing, under MSVC6 i have this error:
error C2653: 'map<int,int,struct std::less<int>,class std::allocator<int> >' : is not a class or namespace name

could someone copy & paste & compile it under VC6 ? I don't know now if it is some strange error in my installation or maybe it just won't compile under VC6?

##### Share on other sites
yeah that is weird...when I compile it with VC6 I get those errors unless I add the std:: But if I compile it with the vc7 toolkit stuff, it's fine. I really don't know why, especially since it only seems to do that with an iterator, and not with other std::map stuff :\

##### Share on other sites
Microsoft Visual C++ 6 sucks. It hardly desires to be called a C++ compiler.

• 10
• 16
• 9
• 13
• 41