#include <iostream> & "using namespace std;"

#include <iostream> & "using namespace std;"

Post by Tetsuya Og » Sat, 26 Jul 2003 12:50:28


Hi all,

I just started learning this spreme language of VC++ using
version 7.0.9037 on Visual Studio .NET (a bit buggy since
this is Beta 1). From my previous post I learnt that I
should use <iostream> instead of <iostream.h>.

Here is my VERY SIMPLE code;
+++
#include <iostream>

using namespace std;

int main (void)
{
cout << "Hello World!";
return 0;
}
+++

1.
The above-mentioned code works without a hitch, but if I
take out "#include <iostream>" directive or "using
namespace std;", the error message says, "error
C2065: 'cout' : undeclared identifier". Why do I need
both? I thought "std" embodies standard input and output
libraries and therefore I don't need #include directive...

2.
Why can I NOT find "std" header file under \Microsoft
Visual Studio.NET\Vc7\include\? All the other header files
are in this "include" folder. Is this "std" something very
special??? Where can I find it?

3.
What is your opinion in using "using namespace std;"
extravagantly? I read some articles saying that we can use
it for a small project, but not for a big one since it
might mask global variables scope unnecessarily (or
unexpectedly), in other words, they suggest
use "std::cout". What do you think?

Thanks for your time,
---
Tetsuya Oguma, Sydney, Australia
 
 
 

#include <iostream> & "using namespace std;"

Post by Bo Persso » Sat, 26 Jul 2003 18:25:35

"Thore B. Karlsen" < XXXX@XXXXX.COM > skrev i meddelandet
news: XXXX@XXXXX.COM ...
std::cout.
(And it
"Namespaces,
way
separate
use
Otherwise,

Unless, of course, you sometimes later decide to use another library
(see 2. above) and that library contains something that clashes with a
standard namespace item. To be fair, writing std::cout isn't that much
of a trouble, is it?

#include <iostream>
#include <ostream>

int main ()
{
std::cout << "Hello World!";
return 0;
}

If you want to be *very* correct, you should also include <ostream>
for output streams. It might or might not be included by <iostream>,
so to be on the safe side (portability!) you should include that
header as well.



Bo Persson
XXXX@XXXXX.COM




 
 
 

#include <iostream> & "using namespace std;"

Post by Thore B. K » Sat, 26 Jul 2003 20:57:32


[using namespace std]



Then you can qualify the names that clash. The point is, a
using-statement will be local to that file. You will not be causing
unexpected side effects in other files.


You will of course want to look beyond this trivial example. I don't see
any benefit in writing std:: thousands of times if it is perfectly safe
not to do it.

--
Be seeing you.