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

hi
i think you have overloaded ">>" operator so that compiler gets confused about which operator should be matched exactly with that operand since >> is a built in operator so you should not overload it.
Hope this will clear your confusion...

## All 3 Replies

hi
i think you have overloaded ">>" operator so that compiler gets confused about which operator should be matched exactly with that operand since >> is a built in operator so you should not overload it.
Hope this will clear your confusion...

I know this but the exercise is like this.
And also here "cin>>n;" is no problem at all.
only with "cin >> t;"

hi
i think you have overloaded ">>" operator so that compiler gets confused about which operator should be matched exactly with that operand since >> is a built in operator so you should not overload it.
Hope this will clear your confusion...

Wait, what?

The issue is that your prototype for >> in your class declaration doesn't match what you have in your defintion.

``friend istream& operator>>(istream& is, Rational[B]&[/B] r);``

vs.

``istream& operator>>(istream& is, Rational r)``

To the compiler, those are two different signatures, one with a reference to a Rational and the other without. Just change the second to `Rational & r` Also, a minor point, the semicolons after your method definitions 46,52,58, etc., are extraneous (when compiled with a low warning level, they are passable, but conventionally they are not placed there).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.