error: 'cout' was not declared in this scope

Linux下C++编译出错原因解析

程序:

#include 
int main()
{
cout << "hello world" << endl;
}

编译出错:
$ g++ s.cpp -o s.out
s.cpp: In function 'int main(int, char**)':

s.cpp:12: error: 'cout' was not declared in this scope
s.cpp:12: error: 'endl' was not declared in this scope

原因:
C++ 1998 要求cout and endl被调用使用'std::cout'和'std::endl'格式,或using namespace std;

修改后:

#include 
int main()
{
std::cout << "hello world" << std::endl;
}

#include 
using namespace std;
int main(int argc, char *argv[])
{
cout << "hello world" << endl;
}

编译通过。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容