【问题标题】:cin as istream object without stream buffer object's referencecin 作为没有流缓冲区对象引用的 istream 对象
【发布时间】:2020-03-16 20:22:00
【问题描述】:

我正在研究一个将cin 作为参数传递的问题,这是我的代码:

#include<istream>
#include<iostream>

void fun(std::istream &os)
{
    int i;
    os>>i;
    std::cout<<i<<std::endl;
}

int main()
{
    fun(std::istream::cin);  //ERROR

    return 0;
}

错误提示:

‘cin’不是‘std::istream’{aka ‘std::basic_istream’}的成员

当它只是 std::cin 时,错误就消失了 - 现在它写在书 (CPP Primer) 中,cin 是一个 istream 对象,但 istream 的构造函数需要引用流缓冲区对象为了被建造。那么cin 是如何在没有任何争论的情况下工作的呢?示例:

cin&gt;&gt;input;

【问题讨论】:

    标签: c++ io


    【解决方案1】:

    std::cin 是一个type 为std::istream 的对象。它的名字是std::cin。它不是std::istream 的成员,所以你不能把它命名为它是。这就是编译器抱怨std::istream::cin 的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-24
      • 2018-01-04
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 2019-11-27
      相关资源
      最近更新 更多