Linux compilation error: ' cout ' not yet declared in this scope

Source: Internet
Author: User

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++;        }   }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.