System header files and the std namespace.
I came across something weird in MS Visual Studio 6 and was wondering if anyone could explain it (and/or see if their own compilers also have this weirdness). It doesn't really matter to me, as it's easily worked around, but I was idly curious and thought I'd ask here.
The following code:
will generate the compiler error "error C2039: 'cin' : is not a member of 'std'" unless it's preceded by
I did some investigation (i.e. looking at the source code for fstream), and found that this is because fstream doesn't include iostream; rather, fstream includes iostream.h. So what? Well, as further investigation (i.e. testing) revealed, iostream.h doesn't define the namespace std; rather, it uses the general namespace.
Now, there are two easy workarounds (a: include iostream myself; b: skip the using std::cin statement, since cin is defined in the general namespace) and one probably-a-bad-idea one (alter fstream such that it includes iostream rather than iostream.h), so I'm not terribly concerned about it. Nonetheless, I was wondering if anyone could explain to me why things are the way they are. I understand why the change from .h to not-.h and why .h's still exist, but I don't understand why fstream would include iostream.h. (Alternatively, if you could link me to a web site that explains it, that would be great as well, but I STW'd and found nothing that seemed relevant.)
Thanks in advance!
The following code:
#include <fstream>
using std::cin;
using std::cin;
will generate the compiler error "error C2039: 'cin' : is not a member of 'std'" unless it's preceded by
#include <iostream>
I did some investigation (i.e. looking at the source code for fstream), and found that this is because fstream doesn't include iostream; rather, fstream includes iostream.h. So what? Well, as further investigation (i.e. testing) revealed, iostream.h doesn't define the namespace std; rather, it uses the general namespace.
Now, there are two easy workarounds (a: include iostream myself; b: skip the using std::cin statement, since cin is defined in the general namespace) and one probably-a-bad-idea one (alter fstream such that it includes iostream rather than iostream.h), so I'm not terribly concerned about it. Nonetheless, I was wondering if anyone could explain to me why things are the way they are. I understand why the change from .h to not-.h and why .h's still exist, but I don't understand why fstream would include iostream.h. (Alternatively, if you could link me to a web site that explains it, that would be great as well, but I STW'd and found nothing that seemed relevant.)
Thanks in advance!
