9

for using cout, I need to specify both:

#include<iostream>

and

using namespace std;

Where is cout defined? in iostream, correct? So, it is that iostream itself is there in namespace std?

What is the meaning of both the statements with respect to using cout?

I am confused why we need to include them both.

4
  • 5
    @Neil Then what is the purpose of SO? To only ask questions that have no way of being answered by reading a book or searching the internet? Commented Apr 15, 2010 at 18:57
  • 1
    @Neil Butterworth: okay! no more stupid questions after this one, without reading the concepts from a book. Commented Apr 15, 2010 at 18:57
  • 6
    @Kevin Anyone that thinks they can learn C++ by asking questions on SO is deluding themselves, and wasting our time. Commented Apr 15, 2010 at 18:59
  • 4
    I think the question is appropriate for SO. SO wasn't meant to be reserved only for obscure/advanced questions. Commented Apr 15, 2010 at 19:20

4 Answers 4

9

iostream is the name of the file where cout is defined. On the other hand, std is a namespace, equivalent (in some sense) to Java's package.

cout is an instance defined in the iostream file, inside the std namespace.

There could exist another cout instance, in another namespace. So to indicate that you want to use the cout instance from the std namespace, you should write

std::cout, indicating the scope.

std::cout<<"Hello world"<<std::endl;

To avoid the std:: everywhere, you can use the using clause.

cout<<"Hello world"<<endl;

They are two different things. One indicates scope, the other does the actual inclusion of cout.

In response to your comment

Imagine that in iostream two instances named cout exist, in different namespaces:

namespace std{
   ostream cout;
}
namespace other{
   float cout;//instance of another type.
}

After including <iostream>, you'd still need to specify the namespace. The #include statement doesn't say "Hey, use the cout in std::". That's what using is for, to specify the scope.

Sign up to request clarification or add additional context in comments.

2 Comments

@Tom: When I already included the file (iostream), I specified which definition of cout to use. Now the compiler knows what defination to use. So, what is the problem then. I am still confused as to why I need to say that it is in std namespace. That should have been necessary if there was some confusion. Here, there is no confusion as there is only one defination of cout.
@cambr: No, you specified a definition of cout to use. There's no reason you couldn't define int cout; afterwards, although it's not a good idea. Arguably, the C++ standard could have required that namespaces be ignored if there's only one namespace with a given symbol, but it didn't, and I can't think of a language using namespaces where it does.
2

If your C++ implementation uses C style header files (many do) then there is a file that contains something similar to:

#include ... // bunches of other things included

namespace std {

... // various things

extern istream cin;
extern ostream cout;
extern ostream cerr;

... // various other things

}

std is the namespace that the C++ standard says most of the standard things should reside in. This is to keep from overpopulating the global namespace, which could cause you difficulty in coming up with names for your own classes, variables, and functions which aren't already used as names for standard things.

By saying

using namespace std;

you are telling the compiler that you want it to search in the namespace std in addition to the global namespace when looking up names. If the compiler sees the source line:

return foo();

somewhere after the using namespace std; line it will look for foo in various different namespaces (similar to scopes) until it finds a foo that meets the requirements of that line. It searches namespaces in a certain order. First it looks in the local scope (which is really an unnamed namespace), then the next most local scope until over and over until outside of a function, then at the enclosing object's named things (methods, in this case), and then at global names (functions, in this case unless you've been silly and overloaded () which I'm ignoring), and then at the std namespace if you've used the using namespace std; line. I may have the last two in the wrong order (std may be searched before global), but you should avoid writing code that depends on that.

Comments

1

cout is logically defined within iostream. By logically, I mean it may actually be in the file iostream or it may be in some file included from iostream. Either way, including iostream is the correct way to get the definition of cout.

All symbols in iostream are in the namespace std. To make use the the cout symbol, you must tell the compiler how to find it (i.e. what namespace). You have a couple of choices:

// explicit
std::cout << std::endl;

// import one symbol into your namespace (other symbols must still be explicit)
using std::cout;
cout << std::endl;

// import the entire namespace
using namespace std;
cout << endl;

// shorten the namespace (not that interesting for standard, but can be useful
// for long namespace names)
namespace s = std;
s::cout << s::endl;

Comments

1

The #include <iostream> references the header file that defines cout. If you're going to use cout, then you will always need the include.

You do not need to using namespace std;. That's simply allows you to use the shorthand cout and endl and the like, rather than std::cout and std::endl where the namespace is explicit. Personally I prefer not to use using namespace ... since it requires me to be explicit in my meaning, though it is admittedly more verbose.

1 Comment

The compromise is using std::cout; using std::endl;, after which you can use cout and endl without qualification, without bringing the whole std namespace into the global namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.