I am currently using Teach Yourself C++ in 21 Days, Second Edition book to learn about C++ coding, along with Microsoft Visual C++ 2010 Express. At the end of Chapter 1, there is a small exercise about writing and compiling the following code:
#include <iostream>
int main ()
{
cout << "Hello World!\n";
return 0;
}
Quite simple, right? However to my surprise the code would not compile, due to this error:
error C2065: 'cout' : undeclared identifier
I began scouring the Web, and soon found some solutions here. Turns out I had to add using namespace std; to my code!
However there was no mention of namespaces in the book, so I figured the book is outdated. (It uses #include <iostream.h> pre-processor directive!) After some more Web research I found a lot of information about namespaces, namespace std, along with some historical background on <iostream.h> and <iostream>, and all this flow of new information is quite confusing to me. (Not to mention all the unnecessary Google results about medical STDs...)
So here are some questions I've got so far:
- If I am including the iostream library, why is a namespace needed to find cout? Is there another cout somewhere that could cause a name clash? If someone could provide a diagram for this, that'd be great.
And as a bonus, some historical background:
-
What exactly was iostream.h before it was changed to iostream?
-
Did namespace play a part in this change?