简体   繁体   English

通过引用传递 std::istream 和 std::ostream 以在函数内使用

[英]Pass std::istream and std::ostream by reference to be used from within function

I am trying to consume the following function:我正在尝试使用以下功能:

void f(std::istream& input, std::ostream& output) {
    int n;
    output << "enter a number: ";
    input >> n;
}

int main() {
    std::istream is;
    std::ostream os;
    f(is, os);
    return 0;
}

Error:错误:

'std::basic_istream<_CharT, _Traits>::basic_istream()

Full error that's the entire error I am getting out when debugging and compiling this,完整错误是我在调试和编译时遇到的整个错误,

c:\Users\root\Documents\cpp\main.cpp: In function 'int main()':
c:\Users\root\Documents\cpp\main.cpp:40:18: error: 'std::basic_istream<_CharT, _Traits>::basic_istream() 
[with _CharT = char; _Traits = std::char_traits<char>]' is protected within this context
   40 |     std::istream in;
      |                  ^~
In file included from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:40,
                 from c:\Users\root\Documents\cpp\main.cpp:7:
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\istream:606:7: note: declared protected here
  606 |       basic_istream()
      |       ^~~~~~~~~~~~~
c:\Users\root\Documents\cpp\main.cpp:41:18: error: 'std::basic_ostream<_CharT, _Traits>::basic_ostream() 
[with _CharT = char; _Traits = std::char_traits<char>]' is protected within this context
   41 |     std::ostream out;
      |                  ^~~
In file included from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from c:\Users\root\Documents\cpp\main.cpp:7:
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:390:7: note: declared protected here
  390 |       basic_ostream()
      |       ^~~~~~~~~~~~~

f(std::cin, std::cout) result in the error below: f(std::cin, std::cout)导致以下错误:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid

Have a look at the std::istream constructor and the std::ostream constructor and example also.也看看std::istream 构造函数std::ostream 构造函数和示例。

You want to do this:你想这样做:

#include<iostream>

void f(std::istream& input, std::ostream& output)
{
    int n;
    output << "enter a number: ";
    input >> n;
}

int main()
{
    f(std::cin, std::cout);
    return 0;
}

Demo演示

instead of this:而不是这个:

void f(std::istream& input, std::ostream& output)
{
    int n;
    output << "enter a number: ";
    input >> n;
}

int main()
{
    std::istream is;   // Note: not linked to console
    std::ostream os;   // Note: not linked to console
    f(is, os);
    return 0;
}

The error message is because you can not access the default constructor of std::istream or std::ostream , it is protected:错误消息是因为您无法访问std::istreamstd::ostream的默认构造函数,它受到保护:

basic_istream : basic_istream :

protected:
  basic_istream()
  : _M_gcount(streamsize(0))
  { this->init(0); }

basic_ostream : basic_ostream :

protected:
  basic_ostream()
  { this->init(0); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM