C++, header files and using namespace std

c

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.

Best Answer

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.