Here's my fraction
class:
class fraction { // type definition
int num;
int denom;
ostringstream sstr;
public:
fraction(int c=0, int d=1) :
num(c), denom(d)
{ sstr = ostringstream(); }
fraction(const fraction &f) : num(f.num), denom(f.denom) { /*void*/ }
friend ostream& operator<<(ostream &os, const fraction &f){
os << "(" << f.num << "/" << f.denom << ")";
return os;
}
friend istream& operator>>(istream &is, const fraction &f){
is >> "(" >> f.num >> "/" >> f.denom >> ")"; // Exception thrown on this line on "is >>"
return is;
}
Overloading the operator<<
works, but operator>>
throws an error:
cannot bind 'std::istream {aka std::basic_istream<char>}' lvalue to 'std::basic_istream<char>&&'
I have looked at other questions here on SO, but still have no idea why this could be. I think it might have something to do with pointers, but I am clueless. Please note that I'm really new to C++, so there might be some obvious flaws in my code, feel free to point them out in a comment.