我正在尝试创建一个使用有理数并对它们执行运算符重载的类。我在程序的一部分上遇到问题,即输入流。
例如,我应该以“12/8”格式输入,它应该将 12 存储到变量 a 中,然后将 8 存储到变量 b 中。
这是我的代码:
istream& operator>>( istream& In, Rational& Item )
{
char division_sign;
int a,b;
In >> a >> division_sign;
if (division_sign != '/' || !In.good())
{
In.setstate( ios::failbit );
}
else
{
In >> b;
if (b != 0 || !In.good())
{
return Item.numerator_ = a, Item.denominator_ = b;
}
}
}
这是我收到的错误:
In function 'std::istream& operator>>(std::istream&, Rational&)':
131: error: invalid initialization of reference of type 'std::istream&' from expression of type 'int'
Line 131
是return
声明