我读到 cout 是 ostream 的对象...
但是为什么这段代码
#include<ostream>
using namespace std;
int main()
{
cout << "ostream included!" << endl;
return 0;
}
抛出错误:-
practice1.cpp: In function 'int main()':
practice1.cpp:6:1: error: 'cout' was not declared in this scope
cout << "ostream included!" << endl;
^~~~
是我理解错了还是有其他错误? (MinGW window 10)
提前致谢!
最佳答案
描述
这不起作用的原因是因为 cout
是 OStream 类型但在 IOStream header 内。因此,要获得 cout
的定义,您需要包含 iostream
库,而不是 ostream
类。
解决方案
包括 iostream
而不是 ostream
,如 OriBS提到。
引用资料
- 对象
cout
在 IOStream objects list 中找到 -
"Including
iostream
automatically includes alsoostream
..." see http://www.cplusplus.com/reference/iostream/ -
"The standard objects cout, cerr and clog are objects of this type." see http://www.cplusplus.com/reference/ostream/ostream/
关于c++ - cout 不使用 ostream 头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47416178/