0

I'm trying to get a good structure in some code I'm writing but I'm not quite sure about header files. One of the problems I have is: I know I'm not supposed to include namespaces in header files but I don't know where else to put it.

Consider this header:

// deck.h
#pragma once
#include <vector>
#include "card.h"
using namespace std;
typedef vector<card> pile;
class deck{
public:
 deck();
 ~deck();
 void shuffle();
 card takeCard();
 int getSize();
private:
 pile cDeck;
};

The code in card.h is the definition of a card struct. Should the typedef even be in the header file? And ifso, how do I avoid using std::vector?

Any tips would be appriciated.

asked May 23, 2015 at 16:17
3
  • using namespace std; in a header file is a pretty bad idea! Commented May 23, 2015 at 16:19
  • You shouldn't avoid #include <vector> or std::vector, you need only avoid using namespace std;. (Also, if the pile type is internal to the class, which seems like a good idea, the typedef should be in the private section of the class.) Commented May 23, 2015 at 16:23
  • using std::vector is a super better option here! Commented Feb 21, 2018 at 15:32

3 Answers 3

1

You don't put it at all. Use std::vector instead of vector, there are only 5 additional characters to type. The typedef is OK to be in header files.

As you probably are aware, using namespace std; in a header is BAD. Why? Because all files that will include your header will automatically use namespace std;, and it's relatively easy to get into conflicting names, especially in large projects where the client may not be aware of the using directive he/she is implicitly using.

Alternatively, you may use namespace std; inside an inline function definition in the header,

inline void f()
{
 using namespace std;
 cout << "bla" << endl;
}

or classes,

class Foo
{
using namespace std;
 // rest
};

This way, the using is effectively "seen" only in its enclosing scope.

Related: What's the scope of the "using" declaration in C++?

answered May 23, 2015 at 16:18

4 Comments

The 5 characters is true for this header, but I can imagine there are much bigger headers in which it could actually be quite a hassle to type it everytime. So even if the header file would be a bigger one, which would use a certain namespace a lot, you would still use xxx::xx? It seems wrong to have to type it multiple times.
@Dirklaren I was just editing actually. You can use namespace std; in a scope, so it won't "leak" outside of it.
@Dirklaren: Some of us use std:: every time. It's really a normal thing to do. The only time I use a namespace directive is with long nested namespaces, like boost::gregorian. Even then, I don't completely remove it, I just come up with an abbreviation for it (e.g. namespace dt = boost::gregorian;), and only in the scope of a function.
"So even if the header file would be a bigger one, which would use a certain namespace a lot, you would still use xxx::xx?" Yes "It seems wrong to have to type it multiple times." It's really a tiny proportion of the things I type.
0

Removing the using directive is trivial, just rewrite the code to

// deck.h
#pragma once
#include <vector>
#include "card.h"
typedef std::vector<card> pile;
class deck{
public:
 deck();
 ~deck();
 void shuffle();
 card takeCard();
 int getSize();
private:
 pile cDeck;
};
answered May 23, 2015 at 16:25

Comments

0

Using and using namespace can be used within functions within your implementation. If you want to limit possible conflicts as mentioned above, and you are in a situation where you really need to use "using" or "using namespace", keep it within small functions.

answered Feb 21, 2018 at 15:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.