The Risk off Using Namespace

So, here we are, writing our first bit of code. We’ve included a library of code in our .cpp file to make our life easier. Everything’s going well.

In our confidence, we decide that writing std::cout every time is just too much hassle. So instead we use the using namespace std instead.

Great! Our codes now shorter. It’s all going well.

Then an email pops up from a friend who is also into programming. He’s been nice enough to send us a library of functions that he has already written and tested. So we include that as well.

But in this library, he has also included a function called cout, which does something completely different from std::cout.

All of a sudden, the compiler becomes confused. It thinks: “You want me to run cout, but which version of cout. There are two now!”

Without the namespace it has become very confusing which cout you are specifying. That’s why it is always good to include the namespace in the code, and not just put it at the top.

Privacy & Terms