Jump to content
  • Advertisement
Sign in to follow this  
TomMarc

using std::*** problem

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by TomMarc
1. 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 this post


Link to post
Share on other sites
Quote:
Original post by TomMarc
just 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<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

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by XXX_Andrew_XXX
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.


Can you say Borland's VCL? Say thankya!

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!