Difference between cout and std::cout?

Apr 13, 2013 at 9:19pm
I dont know when to use "cout" and when to use "std::cout".
Sometimes I get error when I use "cout" and sometimes when I use "std::cout"
Can you help me please?
Apr 13, 2013 at 9:26pm
std is the name of a namespace.
http://cplusplus.com/doc/tutorial/namespaces/

Everything in the C++ library is (or should be) defined inside the std namespace.

You can write simply cout instead of std::cout usually after you've done:
1
2
3
4
5
using namespace std;

// or

using std::cout;


You are advised to avoid using namespace std; and always explicitly use std:: in order to avoid name collisions.

http://www.parashift.com/c++-faq/using-namespace-std.html
Last edited on Apr 13, 2013 at 9:27pm
Apr 13, 2013 at 9:32pm
closed account (zb0S216C)
"cout" implies that you've told the compiler that you're "using" "cout" at some point. Any references to "cout" will cause the compiler to locate that identifier (or symbol) by resolving its scope; the compiler will search both the global name-space and the "std" name-space if you "#included <iostream>".

With "std::cout", however, the compiler knows exactly where to look for "cout". So in effect, qualifying "cout" with "std" helps the compiler by eliminating guess-work.

Wazzak
Last edited on Apr 13, 2013 at 9:33pm
Apr 13, 2013 at 10:05pm
Thank you all for the quick help :)
Topic archived. No new replies allowed.