can someone tell me why thi wont compile?
header file--
friend bool &operator==(Frac &, Frac &);
cpp file--
bool &operator==(Frac &a, Frac &b)
{
if(a.numerator==b.numerator && a.denominator==b.denominator)
return true;
else
return false;
}
can someone tell me why thi wont compile?
header file--
friend bool &operator==(Frac &, Frac &);
cpp file--
bool &operator==(Frac &a, Frac &b)
{
if(a.numerator==b.numerator && a.denominator==b.denominator)
return true;
else
return false;
}
See how this fits.
Friend functions have access to the data members of a class as if they were a class method, which they aren't, being they are defined outside the class. Therefore, assuming the friend function is declared a friend in the public section of the class and the member variables numerator and denominator are both declared with public access, THEN you shouldn't have to pass the object a to itself. Drop all reference to object a in your code and give it a try.
header file--
friend bool &operator==(Frac &);
cpp file--
bool &operator==(Frac &rhs)
{
if(numerator==rhs.numerator && denominator==rhs.denominator)
return true;
else
return false;
}
Then in your code you would use it like this:
Frac a, b;
if(a == b)
//yadda yadda yadda
and the compiler will use b as the right hand side (rhs) of the == operator and a as calling object or left hand side of the == operator.
Thanks for the help..... but im stll getting an error "operator ==(Frac &) must be declaredwith two parameters.... if some one has another suggestion im totally open for it.
thanks in advanceCode://fraction.h #ifndef fraction_h #define fraction_h #include <iostream> class Frac { friend Frac &operator+(Frac &, Frac &); friend Frac &operator*(Frac &, Frac &); friend Frac &operator/(Frac &, Frac &); friend bool &operator==(Frac &); friend istream &operator>>(istream&, Frac &); friend ostream &operator<<(ostream&, Frac &); public: Frac(int=0, int=1); int getNumerator (); int getDenominator (); Frac reduce(Frac); Frac set(int, int); private: int numerator; int denominator; int negative; }; #endif //fraction.cpp #include "fraction.h" #include <iostream> Frac::Frac(int n, int d) { numerator = n; if (d == 0) { cout << "\aDenominator can not be 0\nDenominator set to 1" << endl; denominator = 1; } else denominator = d; } Frac Frac::set(int a, int b) { Frac temp(a,b); return temp; } int Frac::getNumerator() { return numerator; } int Frac::getDenominator() { return denominator; } Frac Frac::reduce(Frac temp) { int a,b,rem; Frac hold; int den=temp.getDenominator(); int num=temp.getNumerator(); b=den; a=num; do{ rem=num%den; num=den; den=rem; }while(rem!=0); a=a/num; b=b/num; hold=hold.set(num,den); return hold; } bool &operator==(Frac &rhs) { if(numerator==rhs.numerator && denominator==rhs.denominator) return true; else return false; } Frac &operator+(Frac &a, Frac &b) { int n =(a.denominator*b.numerator)+(b.denominator*a.numerator); int d =a.denominator*b.denominator; a=a.set(n,d); return a; } Frac &operator*(Frac &a, Frac &b) { int n =a.numerator*b.numerator; int d =a.denominator*b.denominator; a=a.set(n,d); return a; } Frac &operator/(Frac &a, Frac &b) { int temp =a.denominator*b.denominator; int n =(temp/a.denominator)*a.numerator; int d =(temp/b.denominator)*b.numerator; a=a.set(n,d); return a; } istream &operator>>(istream &input, Frac &temp) { cout<<"Please enter the Numerator "; input>>temp.numerator; cout<<"Please enter the Denominator "; input>>temp.denominator; int count; do{ if (temp.denominator == 0) { cout << "\aDenominator can not be 0\nPlease re-enter Denominator "; input>>temp.denominator; count=1; } else { count=-1; } }while(count==1); return input; } ostream &operator<<(ostream &output, Frac &temp) { temp.reduce(temp); int hold; int den=temp.getDenominator(); int num=temp.getNumerator(); if(num>den) { cout<<"1 "; hold = num-den; cout<<hold<<"/"<<den; } if(num<den) { if(den==1) cout<<num; if(den!=1) cout<<num<<"/"<<den; } if(num==den) cout<<1; return output; } //main.cpp #include <iostream> #include "fraction.h" int main() { Frac a,b; cin>>a>>b; if(a==b) cout<<1; return 0; }
Since you've made operator== a global function it needs two Frac objects to compare. As it' s not a member function trying to access a numerator or denominator that aren't part of a Frac object will not work. Try something like
bool &operator==(Frac &lhs,Frac &rhs)
{
if(lhs.numerator==rhs.numerator && lhs.denominator==rhs.denominator)
return true;
else
return false;
}
Alright, time to admit I made a mistake in my first post. I reread the section on friend functions in chapter 15 "Advanced Inheritance" of Liberty's "Teach Yourself C++ in 21 days" available online at:
http://www.stud.fim.ntnu.no/~oystesk/CPP/htm/ch15.htm
It uses two parameters in friend function implementing overloaded + operator for a String class rather than the one parameter version I suggested in my first post (Liberty uses that version as a member function, not a friend function). Liberty declares the friend function with public access rather than the default private access yours appears to have. It also used public accessor functions to obtain the necessary member variables which were declared with private access as yours are. Therefore I would try the following:
prototype
friend bool operator== ( Frac & , Frac &);
definition
bool operator==(Frac & lhs, Frac & rhs)
{
if(lhs.getDenominator() == rhs.getDenominator() &&
lhs.getNumerator() == rhs.getNumerator())
return true;
}
else
return false;
}
I am also not familiar with using a reference to a bool variable so I left that out in the return type.
I would also suggest you log onto the above website and read the appropriate section(s) on friend functions for yourself. I wish I had done it earlier. Good luck.
Thanks for all the help it works now...
-Skeptic