我正在嘗試創建一個類,該類與有理數一起工作並在其上執行運算符重載。我在程序的一部分,輸入流上有問題。從'int'類型的表達式中獲取'std :: istream&'類型的引用的無效初始化
例如,我應該以格式「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
聲明
相關:http://stackoverflow.com/questions/9825768/safer -but-easy-to-use-and-flexible-c-alternative-to-sscanf/9832875#9832875 – 2013-02-26 18:39:33