【问题标题】:cout not working with ostream header filecout 不适用于 ostream 头文件
【发布时间】:2017-11-21 15:00:10
【问题描述】:

我读到 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 在iostream.h中声明
  • 投票结束是一个错字。您的意思是包含 &lt;iostream&gt; 而不是 &lt;ostream&gt;
  • @DigvijaysinhGohil iostream,而不是 iostream.h
  • 以后你可以参考cppreference网站,它会告诉你ostream里面有什么。
  • @DigvijaysinhGohil 错误。标准指定的标头仅命名为iostream,没有扩展名。如果您有一些提供符号链接或其他东西的编译器或库,那是无关紧要的。

标签: c++ cout ostream


【解决方案1】:

说明

这不起作用的原因是因为cout 是 OStream 类型但在 IOStream 标头内。因此,要获得cout定义,您需要包含iostream 库而不是ostream 类。

解决方案

包括iostream 而不是ostream,正如OriBS 提到的那样。

参考文献

【讨论】:

    【解决方案2】:

    你应该包括iostream

    #include <iostream>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 2016-09-13
      • 2018-07-02
      • 1970-01-01
      相关资源
      最近更新 更多