[英]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. 如果您希望它返回字符,则无需为.get()
指定参数。 https://en.cppreference.com/w/cpp/io/basic_istream/get 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 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: 如果要检查流的布尔值是否false
,则可以执行注释中提到的操作,而不是:
while (input.get(c))
which stores the character in c
then checks the bool
value of the returned input stream. 它将字符存储在c
然后检查返回的输入流的bool
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.