美文网首页
error: 'cout' was not declared i

error: 'cout' was not declared i

作者: mCpG | 来源:发表于2017-12-09 15:47 被阅读0次

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

编译通过。

相关文章

网友评论

      本文标题:error: 'cout' was not declared i

      本文链接:https://www.haomeiwen.com/subject/snzuixtx.html