'cout' was not declared in this scope

C++IostreamCout

C++ Problem Overview


I have a C++ program:

test.cpp

#include<iostream>

int main()
{
	char t = 'f';
	char *t1;
	char **t2;
	cout<<t;	//this causes an error, cout was not declared in this scope
	return 0;
}

I get the error: >'cout' was not declared in this scope

Why?

C++ Solutions


Solution 1 - C++

Put the following code before int main():

using namespace std;

And you will be able to use cout.

For example:

#include<iostream>
using namespace std;
int main(){
	char t = 'f';
	char *t1;
	char **t2;
	cout<<t;		
	return 0;
}

Now take a moment and read up on what cout is and what is going on here: http://www.cplusplus.com/reference/iostream/cout/


Further, while its quick to do and it works, this is not exactly a good advice to simply add using namespace std; at the top of your code. For detailed correct approach, please read the answers to this related SO question.

Solution 2 - C++

Use std::cout, since cout is defined within the std namespace. Alternatively, add a using std::cout; directive.

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
Questionuser494461View Question on Stackoverflow
Solution 1 - C++rafalonView Answer on Stackoverflow
Solution 2 - C++Andy ProwlView Answer on Stackoverflow