简体   繁体   中英

I'm getting the error: no suitable conversion function from std::basic_istream<char, std::char_traits<char>> to char exists

I'm trying to overload the operator >> and I want to read each character from an input but i get that error. Here is the code:

istream& operator>>(istream& input, Natural_Big_Number number)
{
    int x;
    input >> x;
    number.set_nr_digits(x);
    char c;
    while ((c = input.get(c)))
    {

    }
}

You need to not specify a parameter to .get() if you want it to return the character. https://en.cppreference.com/w/cpp/io/basic_istream/get

#include <iostream>
using namespace std;

istream& operator>>(istream& input, int number)
{
    char c;
    while ((c = input.get()))
    {

    }
}

https://godbolt.org/z/XSKvv4

If what you want is to check the boolean value of the stream for false ness, then you would do what was mentioned in the comments, instead:

while (input.get(c))  

which stores the character in c then checks the bool value of the returned input stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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