Message-ID: <19971031213656.18761.qmail@hotmail.com> From: "John Patton" To: djgpp AT delorie DOT com Cc: nick14 AT erols DOT com Subject: Re: I need help with a fraction class Content-Type: text/plain Date: Fri, 31 Oct 1997 13:36:55 PST Precedence: bulk >I've been working on this for a while and I was hoping someone >could explain these errors. > > >#include "bool.h" > >class fractype{ >public: > void write() const; > float floatequiv(); > void simplify(); > fractype operator*(fractype frac2) const; you should return a reference to fractype... and the function should be a *_friend_* of class fractype, not a member: friend fractype & operator*(const fractype & frac1, const fractype & frac2); > boolean operator==(fractype frac2) const; Try bool here. Your implementation is a little messed up... Should be as above: friend bool operator==(const fractype & frac1, const fractype & frac2); > fractype (int initnumer, int initdenom); Usually there will also be a default constructor here like: fractype(){} >private: > int numer; > int denom; >}; > you're missing several operators, BTW. operator=, operator+=, operator!=, operator<<, operator>>, ... to name a few. when you get started on these and need some assistance, let me know... > >#include >#include >#include "fraction.h" > >int GreatestCommonDivisor( int,int); > >fractype::fractype(int initnumer, int initdenom) >{ >numer=initnumer; >denom=initdenom; >} > >void fraction::write() const; ^^^^^^^^ ^ shouldn't it be: void fractype::write() const // without the semicolon?? >{ >cout<} ^^^^^^^^^^^^^^^^^^^^^^^^^ also, a better way would be to overload the << operator. > >float fractype::floatequiv() >{ >return float(numer) / float(denom); >} > >void fractype::simplify() >{ >int gcd; >int absnumer= abs(numer); > >if (numer==0 || absnumer==1 || denom==1) > { > return; > } > >gcd=GreatestCommonDivisor(absnumer, denom); >if (gcd>1) { > numer /= gcd; > denom /=gcd; > } >} > >fractype fractype::operator*(fractype frac2) const >{ >int resultnumer = numer * frac2.numer; >int resultdenom = denom * frac2.denom; > >fractype resultfrac(resultnumer, resultdenom); >resultfrac.simplify(); >return resultfrac; >} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Here I will be a little generous and place my own personal version of this function... (remember it's a friend of our class, BTW, so it can access our private data!) You may or may not like it: fractype & operator*(const fractype & frac1, const fractype & frac2) { int resultnumer = frac1.numer * frac2.numer; int resultdenom = frac1.denom * frac2.denom; fractype resultfrac(resultnumer, resultdenom); resultfrac.simplify(); return resultfrac; } > >boolean fractype::operator==(fractype frac2) const >{ >return (numer==frac2.numer) && (denom==frac2.denom); >} Again, I'm feeling generous... bool operator==(const fractype & frac1, const fractype & frac2) { return ((frac1.numer==frac2.numer) && (frac1.denom==frac2.denom)); } > >int GreatestCommonDivisor(int a, int b) >{ >int temp = a % b; >while (temp>0) { >a=b; >b=temp; >temp=a%b; >} >return b; >} > > >In file included from fraction.cpp:3: >fraction.h:9: syntax error before `operator' >fraction.cpp:13: parse error before `::' >fraction.cpp:50: parse error before `::' >fraction.cpp:51: no `int fractype::operator ==(class fractype) >const' member function declared in class `fractype' I think this should give you a start. Please correct my mistakes if you find any. Thanks, John Patton ______________________________________________________ Get Your Private, Free Email at http://www.hotmail.com