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?

6
  • 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
    Commented Jun 8, 2012 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... Commented Jun 8, 2012 at 14:44
  • 1
    Sorry Luc, that coding in language <x> is not limited the small group of people who were listening to introductory courses about that language.
    – erikbstack
    Commented Jun 9, 2012 at 13:34
  • 2
    @erikb85 I think his complaint was aimed at people who DID attend the course!
    – user146043
    Commented Apr 29, 2014 at 12:56
  • 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). My 3 years old rant was aimed at C++ introductory courses and examples that systematically does that, thereby instiling bad habits in C++ learners. Commented Jun 30, 2015 at 9:20

7 Answers 7

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.

2
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.

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.

0
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?

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.

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.

1
  • 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." Commented Jun 8, 2012 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.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.