유리수에서 작동하고 연산자 오버로딩을 수행하는 클래스를 만들려고합니다. 프로그램의 한 부분 인 입력 스트림에 문제가 있습니다.'int : int'형식의 표현식에서 'std :: istream &'형식의 참조 초기화가 유효하지 않습니다.
예를 들어 "12/8"형식의 입력을 받아야하고 변수 a에 12를 저장하고 변수 b에 8을 저장해야합니다. 여기
내 코드입니다 : 여기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 -c-alternative-to-sscanf/9832875 # 9832875 –