tags:

views:

185

answers:

3
#include <iostream>
using namespace std;

int main() {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    return 0;
}

If I remove the 2nd statement,the build will fail.

Why is it necessary?

+11  A: 

Because cout and endl are contained inside the std namespace.

You could remove the using namespace std line and put instead std::cout and std::endl.

Here is an example that should make namespaces clear:

Stuff.h:

namespace Peanuts
{
  struct Nut
  {
  };
}


namespace Hardware
{
  struct Nut
  {
  };
}

When you do something like using namespace Hardware you can use Nut without specifying the namespace explicitly. For any source that uses either of these classes, they need to 1) Include the header and 2) specify the namespace of the class or put a using directive.

The point of namespaces are for grouping and also to avoid namespace collisions.

Edit for your question about why you need #include :

#include <iostream> includes the source for cout and endl. That source is inside the namespace called std which is inside iostream.

Brian R. Bondy
Alternatively, you could omit the `using namespace std` and instead change `cout` and `endl` to `std::cout` and `std::endl`.
Tyler McHenry
Then what's `iostream` for?Is std defined in iostream?
symfony
You are including the `iostream` header, however `cout` and `endl` are still in the std namespace (`std::cout` and `std::endl`). `iostream` defines things in std, but it's not the only header to do so. E.g. `std::string` is defined in `string`. The `using` directive imports everything from the std namespace.
Matthew Flaschen
`iostream` is to get the declarations of the stream-related objects and functions. Those objects and functions are declared with the `std` namespace, so you either have to use the namespace or qualify the names.
Tyler McHenry
Yes, cout/cerr are defined in iostream, but in the namespace std:: Namespaces are just a way of keeping common names like min/max separate in different packages. It's best to use std::cout directly rather than the 'using' statement
Martin Beckett
What's the reason for iostream to wrap count in std namespace?
symfony
@symfony: For 2 reasons, to show easily that your code is using something from the standard library, and so that the standard library will not conflict with your other source code class names and etc.
Brian R. Bondy
@Matthew Flaschen,so there can be 2 files defines things in the same namespace?
symfony
@symfony: Yes that's fine. You can have any number of files that define things in the same namespace. And any number of files that uses a namespace.
Brian R. Bondy
@Brian R. Bondy,what if there is conflict ?
symfony
@symfony: By conflict maybe you mean the same thing is defined multiple times in the same namespace? then you'll get a compiling error.
Brian R. Bondy
Is it weird?Isn't it more common to let the later definition overwrite the prior?
symfony
I think it is sound.
Brian R. Bondy
@symfony: That's certainly an option, but almost never what's intended. You'd end up with lots of problems down the line.
GMan
+1  A: 

cout is part of the namespace std. Now if you were to use "std::cout" and delete the second line, then it will compile.

Ian
and endl -> std::endl
corn3lius
A: 

Yes cout and cerr are defined in isotream, but as std::cout and std::cerr

The reason for this is that you can happily use common words like min or max without worryign that some standard library has already sued them, simply write std::min and std::max. This is no different from the old way of putting eg 'afx' in front of all the ATL library function.

The 'using' statement is because people complained about the extra typing, so if you put 'using std' it assumes you meant std:: in front of everything that comes from standard.
The only problem is if you have a library called mystuff that also has a min() or max(). If use use std::min() and mystuff::min() there is no problem, but if you put 'using std' and 'using mystuff' you are back to the same problem you had in 'c'

ps. as a rule it is good practice to put std::cout just to make it clear to people that this is the regualr standard version and not some local version of cout you have created.

Martin Beckett