COUT不使用Ostream标头文件

cout not working with ostream header file

本文关键字:文件 Ostream COUT      更新时间:2023-10-16

我读到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 Windows 10)

预先感谢!

描述

原因不起作用的原因是cout是类型Ostream,但位于iostream标题内。因此,要获取cout定义,您需要包括iostream库,而不是ostream类。

解决方案

包括iostream而不是ostream,如提到的Oribs。

参考

  • 在iostream对象列表中找到的对象cout
  • "包括iostream自动还包括ostream ...

  • "标准对象cout,cerr和clog是这种类型的对象。"请参阅http://www.cplusplus.com/reference/ostream/ostream/

您应该包括iostream

#include <iostream>