-1

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:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
John Smith
  • 465
  • 4
  • 15
  • 38

1 Answers1

3

Have a look at the std::istream constructor and the std::ostream constructor and example also.

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:

basic_istream :

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

basic_ostream :

protected:
  basic_ostream()
  { this->init(0); }
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25
  • `f(std::cin, std::cout)` results in => `terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid` – John Smith Sep 05 '20 at 17:21
  • The code compiles, so did it do before, but it throws the error I wrote about above. – John Smith Sep 05 '20 at 17:23
  • Yes, I understand that part. So, how can I consume istream and ostream, to take input and push it out to console with ostream? – John Smith Sep 05 '20 at 17:27
  • @JohnSmith std::cin and std::cout. That's the instances of ostream and istream you have to use. No OSes I know support more than one console per process, and it already is opened, C++ associates it with those streams – Swift - Friday Pie Sep 05 '20 at 17:33
  • @JohnSmith Why you don't want to use std::cout or std::cin, I did not tried before to override std::cout or std::cin. see [this](https://stackoverflow.com/questions/18975512/how-to-override-cout-in-c#:~:text=cout%20is%20normally%20implemented%20as,would%20make%20your%20code%20obtuse.) and [this](https://stackoverflow.com/questions/366955/obtain-a-stdostream-either-from-stdcout-or-stdofstreamfile). – srilakshmikanthanp Sep 05 '20 at 17:51