C++ 'cout' 未在此范围内声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15185801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'cout' was not declared in this scope
提问by rafalon
I have a C++ program:
我有一个 C++ 程序:
test.cpp
测试.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;
}
I get the error:
我收到错误:
'cout' was not declared in this scope
'cout' 未在此范围内声明
Why?
为什么?
回答by rafalon
Put the following code before int main()
:
将以下代码放在前面int main()
:
using namespace std;
And you will be able to use cout
.
您将能够使用cout
.
For example:
例如:
#include<iostream>
using namespace std;
int main(){
char t = 'f';
char *t1;
char **t2;
cout<<t;
return 0;
}
Now take a moment and read up on what cout is and what is going on here: http://www.cplusplus.com/reference/iostream/cout/
现在花点时间阅读一下 cout 是什么以及这里发生了什么:http: //www.cplusplus.com/reference/iostream/cout/
Further, while its quick to do and it works, this is not exactly a good advice to simply add using namespace std;
at the top of your code. For detailed correct approach, please read the answers to this related SO question.
此外,虽然它可以快速完成并且可以工作,但这并不是简单地using namespace std;
在代码顶部添加的好建议。有关详细的正确方法,请阅读此相关 SO 问题的答案。
回答by Andy Prowl
Use std::cout
, since cout
is defined within the std
namespace. Alternatively, add a using std::cout;
directive.
使用std::cout
, 因为cout
是在std
命名空间中定义的。或者,添加using std::cout;
指令。