我读过cout是ostream的一个对象......
但为什么这个代码
#include<ostream>
using namespace std;
int main()
{
cout << "ostream included!" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
抛出错误: -
practice1.cpp: In function 'int main()':
practice1.cpp:6:1: error: 'cout' was not declared in this scope
cout << "ostream included!" << endl;
^~~~
Run Code Online (Sandbox Code Playgroud)
我的理解是错误还是有其他错误?(MinGW windows 10)
提前致谢!
为什么这不起作用的原因是因为cout是OStream类型但是在IOStream标头内.因此,要获得定义,cout您需要包含iostream库而不是ostream类.
正如OriBS所提到iostream的ostream,包括而不是.
cout中发现的iostream对象名单"包括
iostream自动也包括ostream......"请参阅http://www.cplusplus.com/reference/iostream/
"标准对象cout,cerr和clog是这种类型的对象." 见http://www.cplusplus.com/reference/ostream/ostream/