67

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
erikbstack
  • 12,878
  • 21
  • 81
  • 115
  • 1
    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 is – pengibot Jun 08 '12 at 13:49
  • 47
    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... – Luc Touraille Jun 08 '12 at 14:44
  • 1
    Sorry Luc, that coding in language is not limited the small group of people who were listening to introductory courses about that language. – erikbstack Jun 09 '12 at 13:34
  • 2
    @erikb85 I think his complaint was aimed at people who DID attend the course! –  Apr 29 '14 at 12:56
  • @LucTouraille why is it bad to use the namespace std??! – Black Jun 30 '15 at 08:42
  • 7
    @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](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). My 3 years old rant was aimed at C++ introductory courses and examples that systematically does that, thereby instiling bad habits in C++ learners. – Luc Touraille Jun 30 '15 at 09:20

7 Answers7

132

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.

FatalError
  • 52,695
  • 14
  • 99
  • 116
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.

Matthew Walton
  • 9,809
  • 3
  • 27
  • 36
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.

mfontanini
  • 21,410
  • 4
  • 65
  • 73
6

You could use the namespace

http://www.daniweb.com/software-development/cpp/threads/109029/what-its-the-use-of-using-namespace-std

But you might offend someone

Why is "using namespace std" considered bad practice?

Community
  • 1
  • 1
posttool
  • 69
  • 3
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.

Hans Z
  • 4,664
  • 2
  • 27
  • 50
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.

Edmund
  • 697
  • 6
  • 17
  • 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." – John Dibling Jun 08 '12 at 14:00
-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.

jotasi
  • 5,077
  • 2
  • 29
  • 51