我有一个 C++ 程序:
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;
}
我收到错误:
'cout' was not declared in this scope
为什么?
最佳答案
将以下代码放在int main()
之前:
using namespace std;
您将能够使用 cout
。
例如:
#include<iostream>
using namespace std;
int main(){
char t = 'f';
char *t1;
char **t2;
cout<<t;
return 0;
}
现在花点时间阅读一下 cout 是什么以及这里发生了什么:http://www.cplusplus.com/reference/iostream/cout/
此外,虽然它可以快速完成并且有效,但这并不是一个在代码顶部简单地添加 using namespace std;
的好建议。有关详细的正确方法,请阅读此 related SO question 的答案.
关于c++ - 'cout' 未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15185801/