Beginners in Linux for C + + programming will encounter "'cout ' in this scope has not been declared " error.
Many people will find it very strange, I am in strict accordance with C + + syntax to write, why also at compile-time prompt "' cout ' in this scope has not declared" error?
The following is a detailed analysis of the causes of the error, through analysis to get the solution to the problem .
First, let's take a piece of code for example.
If we save this code as Hello.cpp
In the terminal input g++ hello.cpp-o Hello
The compiler is sure to quote "' cout ' has not declared an error in this scope."
Error Reason:
#include can not use Cout/cin in the program directly, #include to include the namespace std to directly use cout/cin, otherwise the function/variable defined in the header file should be preceded by std:: to represent the calling function/ The source of the variable.
Solution:
Method One: Under #include add a phrase "using namespace std;"
[CPP] #include <iostream> using namespace std; int Main (void) { int i; int n=1; for (i=0;i<n;i++) {cout<< "hello\n"; n++; } }
Method Two: Replace cout with cout std::cout [cpp] #include <iostream> int main ( void ) { int i; int n=1; for (i=0;i<n;i++) { std::cout<< "hello\n"; n++; } }