So I understand that one must not use

using namespace std;

in header files. Well for my assignment we have to overload the >> operator. I got my program to run and all, but I had to include the

using namespace std;

Is it okay to do it this way? I attempted to overload the operator without including that command in the header file, but I got an error.

  • 2
    From code review and Stack Overflow – user40980 Oct 14 '15 at 23:32
  • 5
    You never have to pull in the entire namespace std at global scope and as you already know, you shouldn't. Surely there is a solution for your assignment that makes do without using namespace std;. If the linked answers don't help you solve the problem, you may consider asking a question on Stack Overvlow that includes your actual code and ask how to re-arrange it such that it works without using namespace std;. I bet all you'll have to do is replace ostream with std::ostream and so forth. – 5gon12eder Oct 15 '15 at 0:15
  • 2
    Why didn't you just explicitly write std::ostream, etc.? This is always an alternative to using namespace std;. – Davislor Oct 15 '15 at 0:21
  • @Lorehead reviewing the code, given that it is working and bug free, would likely be a better fit for CodeReview over SO. – user40980 Oct 15 '15 at 0:57
  • 1
    @MichaelT “How can I modify this code so that the using namespace std; can be omitted?” would be a good fit for Stack Overflow, I think, unless you want a full review of it. But in any case, we have to see the code. – 5gon12eder Oct 15 '15 at 2:47

well ... not really. What are the parameter of the << operator you are going to overload?

If you are just providing your class a way to be written on a stream, you have to chances:

  1. inside of your namespace, declare a std::ostream& operator<<(std::ostream& stream, const yourclass& intance) or...
  2. inside the std namespace declare ostream& operator<<(ostream& stream, const yournamespace::yourclass& instance) whatever is more convenient for the inside of your function body.

There is no need to have << as global, since ADL will find it anyway, when it is placed between object whose types match.


C++ does resolution by name, not by signature. That means that if two functions/variables/member variables in the same scope have the same name, they can conflict. When you do something like using namespace std;, you are including the entire std namespace in your code.

What would happen if you had a templated function called count in your code that took 3 parameters like std::count does? You would get a conflict because (after name mangling) both functions would have the same name in the same scope. It is better to just include the parts of the std namespace that you want to use, so you don't introduce potential errors into your code when you go back to update it.

So, just including statements like using std::cout; or using std::ostream; will only include those functions/types/typedefs/&c. in your code.

  • Not by signature? – Basilevs Jun 12 '16 at 19:08
  • @Basilevs Not entirely. It first looks up by name to determine if it can resolve the function, then by signature. Though if you are dealing with classes and inheritance, this can get messy because it will do name resolution only, even if the corresponding method with the proper signature would be inherited from a super class (see here). – callyalater Jun 13 '16 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.