Why std::cout instead of simply cout?

C++Iostream

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Solution 3 - C++

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.

Solution 4 - C++

Solution 5 - C++

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.

Solution 6 - C++

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

Solution 7 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionerikbstackView Question on Stackoverflow
Solution 1 - C++FatalErrorView Answer on Stackoverflow
Solution 2 - C++Matthew WaltonView Answer on Stackoverflow
Solution 3 - C++mfontaniniView Answer on Stackoverflow
Solution 4 - C++posttoolView Answer on Stackoverflow
Solution 5 - C++Hans ZView Answer on Stackoverflow
Solution 6 - C++EdmundView Answer on Stackoverflow
Solution 7 - C++suman debView Answer on Stackoverflow