I try to make an exercise from a book and I have only one error.
On this line
cin >> t;
it writes me that "more than one operator >> matches this operand" I know that defined this operand before in another way but I don't know how to solve this.
#include<iostream>
#include<iomanip>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class Rational {
int ns, nj; //counter and denominator
public:
int cmmdc(int a, int b);
Rational(int n1= 0, int n2=1) {
ns=n1; nj=n2;
}
Rational(Rational& r) {
ns=r.sus();
nj=r.jos();
};
int sus()
const {
return ns;
}
int jos ()
const {
return nj;
}
void setsus(int x) {ns = x; }
void setjos(int y) {nj = y; }
Rational& operator+=(const Rational& r);
Rational& operator-=(const Rational& r);
Rational& operator*=(const Rational& r);
Rational& operator/=(const Rational& r);
friend istream& operator>>(istream& is, Rational& r);
friend istream& operator<<(istream& os, Rational& r);
friend int operator == (const Rational& r1, const Rational& r2);
friend Rational operator+(const Rational& r1, const Rational& r2);
friend Rational operator-(const Rational& r1, const Rational& r2);
friend Rational operator/(const Rational& r1, const Rational& r2);
friend Rational operator*(const Rational& r1, const Rational& r2);
friend void simplifica (Rational& r);
};
Rational& Rational :: operator+=(const Rational& r) {
int t=ns*r.jos() + nj*r.sus();
nj=nj+r.jos();
ns=t;
return *this;
};
Rational& Rational :: operator-=(const Rational& r) {
int t=ns*r.jos() - nj*r.sus();
nj=nj+r.jos();
ns=t;
return *this;
};
Rational& Rational ::operator*=(const Rational& r) {
ns *=r.sus();
nj *=r.jos();
return *this;
};
Rational& Rational ::operator /=(const Rational& r) {
ns /=r.sus();
nj /=r.jos();
return *this;
};
int Rational::cmmdc(int a, int b) {
int r;
do {
r=a%b;
a=b;
b=r;
}
while(r);
return a;
};
istream& operator>>(istream& is, Rational r) {
int x, y;
is>>x>>y;
r.setsus(x);
r.setjos(y);
return is;
};
ostream& operator<<(ostream& os, const Rational& r) {
os<<r.sus() << "/" <<r.jos();
return os;
} ;
int operator ==(Rational& r1, Rational& r2) {
simplifica (r1);
simplifica (r2);
return r1.sus() == r2.sus() && r1.jos() == r2.jos();
};
Rational operator +=( Rational& r1, Rational& r2) {
Rational r(r1);
r +=r2;
return r;
} ;
Rational operator -=( Rational& r1, Rational& r2) {
Rational r(r1);
r -=r2;
return r;
} ;
Rational operator *=( Rational& r1, Rational& r2) {
Rational r(r1);
r *=r2;
return r;
} ;
Rational operator /=( Rational& r1, Rational& r2) {
Rational r(r1);
r /=r2;
return r;
} ;
void simplifica(Rational& r) {
int s=r.sus();
int j=r.jos();
int c=r.cmmdc(s, j);
s /=c;
j /=c;
r.setsus(s);
r.setjos(j);
}
int main () {
Rational suma, t;
int n; // number of terms
cout<<"Number of terms= ";
cin>>n;
cout<<"Introduce the fractions one on a line: "<<endl;
for (int i=0; i<n; i++) {
cin >> t;
simplifica(t);
suma += t;
simplifica(suma);
}
cout<<suma<<endl;
system("pause");
return 0;
}
Thank you