Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,109 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,695 people online right now. Registration is fast and FREE... Join Now!




Overloadong <

 
Reply to this topicStart new topic

Overloadong <

ibaraku
14 Oct, 2007 - 06:14 PM
Post #1

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 164



Thanked: 1 times
My Contributions
I'm trying to overload < but I keeo getting the same errors,

Fraction.cpp
C:\Documents and Settings\Julio\My Documents\HWK!\CSC213\HWK3\Fraction.cpp(51) : error C2451: conditional expression of type 'const class Rational' is illegal
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Documents and Settings\Julio\My Documents\HWK!\CSC213\HWK3\Fraction.cpp(52) : error C2143: syntax error : missing ';' before 'string'
Error executing cl.exe.

this is what I have:
friend const Rational operator < (const Rational &fraction1, const Rational &fraction2);//in a fraction class.

const Rational operator < (const Rational &fraction1, const Rational &fraction2){
int upperval1, upperval2;
upperval1=(fraction1.m_numerator)*(fraction2.m_denominator);
upperval2=(fraction2.m_numerator)*(fraction1.m_denominator);
return Rational(upperval1, upperval2);

}

and I try to use it in main like these

if(fraction1<fraction2){
etc...
}

any tips???

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Overloadong <
14 Oct, 2007 - 06:23 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
you should probably be returning a bool (true or false) rather than a Rational(upperval1, upperval2), depending on what the result of the inequality is. without the rest of your class declaration/definition, it's a little hard to tell.

and please post code using the code tags (read the big gray text on the background of the reply box); it makes it way easier to look at and copy/paste your code that way.

-jjh
User is offlineProfile CardPM
+Quote Post

ibaraku
RE: Overloadong <
14 Oct, 2007 - 06:34 PM
Post #3

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 164



Thanked: 1 times
My Contributions
QUOTE(jjhaag @ 14 Oct, 2007 - 07:23 PM) *

you should probably be returning a bool (true or false) rather than a Rational(upperval1, upperval2), depending on what the result of the inequality is. without the rest of your class declaration/definition, it's a little hard to tell.

and please post code using the code tags (read the big gray text on the background of the reply box); it makes it way easier to look at and copy/paste your code that way.

-jjh


Oh I see, I will try to do it using bool, this is the class def.
CODE

class Rational{
public:
    Rational(int numerator, int denominator);
    Rational(int wholenumber);
    Rational();//default constructor

public:

    void normalize();//will reduce fraction
    friend ostream& operator << (ostream& os, const Rational& fraction);//will allow us to output fractions in the form x/y
    friend istream& operator >> (istream& is, Rational& fraction);//will allow us to input a fraction in the form x/y
    friend bool operator == (Rational &fraction1, Rational &fraction2);
    friend bool operator < (Rational &fraction1, Rational &fraction2);
    friend const Rational operator + (const Rational &fraction1, const Rational &fraction2);//we can add fractions a/b + c/d
    friend const Rational operator - (const Rational &fraction1, const Rational &fraction2);//we can substract fractions a/b - c/d
    friend const Rational operator * (const Rational &fraction1, const Rational &fraction2);//we can multiply fractions a/b * c/d
    friend const Rational operator / (const Rational &fraction1, const Rational &fraction2);//we can divide fractions a/b / c/d
    friend const Rational operator < (const Rational &fraction1, const Rational &fraction2);//will compare both fractions
    friend const Rational operator <= (const Rational &fraction1, const Rational &fraction2);//will compare both fractions
    friend const Rational operator > (const Rational &fraction1, const Rational &fraction2);//will compare both fractions
    friend const Rational operator >= (const Rational &fraction1, const Rational &fraction2);//will compare both fractions
    Rational operator ++ (int fraction);//postfix
    Rational& operator ++();//prefix
    Rational operator -- (int fraction);//postfix
    Rational& operator -- ();//postfix
    int GCD(int num, int den);//function to get the greatest common denominator of the fraction

private:

    int factor;//will define in constructor in order to obtain GCD of the fractions
    int m_numerator;//member numerator
    int m_denominator;//member denominator
};//end class


User is offlineProfile CardPM
+Quote Post

ibaraku
RE: Overloadong <
14 Oct, 2007 - 06:41 PM
Post #4

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 164



Thanked: 1 times
My Contributions
I just tried it with bool and it is correct, Thanks a lot for your help man =)
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:28PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month