Why std :: cout instead of just cout?

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 completing this proposal, everything is in order. Now I am curious why I had to do this. We used to use C ++ in classes, but I never had to write std:: before any of these commands. What could be different in this system?

+48
c ++ iostream
Jun 08 2018-12-12T00:
source share
7 answers

It seems your class may have used pre-standard C ++. An easy way to say this is to look at your old programs and see if you see:

 #include <iostream.h> 

or

 #include <iostream> 

The first is standard, and you can just say cout as opposed to std::cout without any extra. You can get the same behavior in standard C ++ by adding

 using std::cout; 

or

 using namespace std; 

Just one idea. Anyway.

+101
Jun 08 2018-12-12T00:
source share

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

 using namespace std; 

in your code to get to it.

However, this is not always the case, and in the past cout was only in the global namespace (or, later, in the global and std ). So I came to the conclusion that your classes used the older C ++ compiler.

+24
Jun 08 2018-12-12T00:
source share

Everything in the standard template library / Iostream is in the std namespace. You probably used:

 using namespace std; 

In your classes and why it worked.

+12
Jun 08 2018-12-12T00:
source share

You can use namespace

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

But you can insult someone

Why is namespace std used? considered bad practice?

+4
Jun 08 2018-12-12T00:
source share

You probably had using namespace std; in your code that you did in the class. This explicitly tells the precompiler to look for characters in std , which means you don't need std:: . Although this is good practice for std::cout instead of cout , so you explicitly call std::cout every time. That way, if you use another library overriding cout , you still have std::cout behavior, and not some other behavior.

+2
Jun 08 2018-12-12T00:
source share

"std" is the namespace used for STL (standard template library). See http://en.wikipedia.org/wiki/Namespace_(computer_science)#C.2B.2B

You can either write "use namespace std;" before using any stl functions, variables or just inserting "std ::" in front of them.

+2
Jun 08 2018-12-12T00:
source share

if they work in ROOT, you don’t even need to write #include and use the std namespace; just start with int filename (). just try it.

-2
Aug 18 '17 at 6:16
source share



All Articles