0
Now after much research about the purpose of using namespace std in c++
I understood the purpose of it is prevent a problem of names collision this problem a result of tow or more variables or functions have the same name for example the a variable is called xyz and another have the same name In hello world program when I don’t use use namespace std Produces an error although there no variable or function have the name where did the error come from? And I searched about this problem all websites say because the function is defined in namespace std Isn’t everything is defined in the header file for example “iostream “ in hello world program? Please help and thanks
1 Resposta
+ 2
If you open any standard library header you'll see that it always starts with "namespace std{ ... }" with everything else within that namespace.
The iostream header has objects like "cerr" and "cout" declared within this namespace for you to use.
In order to access one of these objects/classes/etc you have to tell the compiler where to look.
It does not automatically look inside the std namespace. ( this is basically what "using namespace std;" does )
In order to do this you prefix it with "std::".
For example:
cout -> std::cout
cin -> std::cin
endl -> std::endl
string -> std::string
etc