Why std::cout instead of simply cout?
Asked Answered
K

7

69

I get these error messages for all cout and endl:

main.cc:17:5: error: ‘cout’ was not declared in this scope
main.cc:17:5: note: suggested alternative:
/usr/include/c++/4.6/iostream:62:18: note:   ‘std::cout’

After following the suggestion, everything is fine. Now I am curious, why I had to do that. We used C++ in classes before, but I never had to write a std:: before any of those commands. What might be different on this system?

Koon answered 8/6, 2012 at 13:47 Comment(6)
you have to say you are using std for it to know what cout is. if you do this as an import you can simply say cout, otherwise you have to fully justify it to tell the compiler what cout actuallt isHeterograft
Congratulations, you are the 1000000th victim of the stupid habit of importing the std namespace that every single introductory course to C++ I've ever seen seems to adopt...Wileen
Sorry Luc, that coding in language <x> is not limited the small group of people who were listening to introductory courses about that language.Koon
@erikb85 I think his complaint was aimed at people who DID attend the course!Rally
@LucTouraille why is it bad to use the namespace std??!Cocainize
@EdwardBlack It is not bad to use the namespace std, on the contrary. It is bad to import all the names it contains (with a using namespace std directive) just to avoid typing the std:: prefix, because this defeats the very purpose of a namespace. (see this faq). My 3 years old rant was aimed at C++ introductory courses and examples that systematically does that, thereby instiling bad habits in C++ learners.Wileen
I
135

It seems possible your class may have been using pre-standard C++. An easy way to tell, is to look at your old programs and check, do you see:

#include <iostream.h>

or

#include <iostream>

The former is pre-standard, and you'll be able to just say cout as opposed to std::cout without anything additional. You can get the same behavior in standard C++ by adding

using std::cout;

or

using namespace std;

Just one idea, anyway.

Incubus answered 8/6, 2012 at 13:49 Comment(2)
Most complete and most readable answer! I even had the <iostream.h> problem!Koon
Please don't use using namespace std;, e.g. see #1453221Haler
D
25

In the C++ standard, cout is defined in the std namespace, so you need to either say std::cout or put

using namespace std;

in your code in order to get at it.

However, this was not always the case, and in the past cout was just in the global namespace (or, later on, in both global and std). I would therefore conclude that your classes used an older C++ compiler.

Duwe answered 8/6, 2012 at 13:49 Comment(0)
S
12

Everything in the Standard Template/Iostream Library resides in namespace std. You've probably used:

using namespace std;

In your classes, and that's why it worked.

Slovakia answered 8/6, 2012 at 13:48 Comment(0)
G
3

You probably had using namespace std; before in your code you did in class. That explicitly tells the precompiler to look for the symbols in std, which means you don't need to std::. Though it is good practice to std::cout instead of cout so you explicitly invoke std::cout every time. That way if you are using another library that redefines cout, you still have the std::cout behavior instead of some other custom behavior.

Garth answered 8/6, 2012 at 13:49 Comment(0)
A
1

"std" is a namespace used for STL (Standard Template Library). Please refer to https://en.wikipedia.org/wiki/Namespace#Use_in_common_languages

You can either write using namespace std; before using any stl functions, variables or just insert std:: before them.

Allophane answered 8/6, 2012 at 13:51 Comment(1)
Pedantic, perhaps: It's actually the C++ Standard Library elements, not just the STL, that are in namespace std. Reference C++03:17.4.1.1/2: "All [C++ Standard] library entities except macros, operator new and operator delete are defined within the namespace std or namespaces nested within namespace std."Addict
C
-4

If you are working in ROOT, you do not even have to write #include<iostream> and using namespace std; simply start from int filename().

This will solve the issue.

Camargo answered 18/8, 2017 at 6:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.