overloaded bool operator

can someone please tell my why this is not working? It is telling me "expected primary expression before else" I think it is just returning the value that i type in my return statement?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{      
 Rational test1, test2(10), test3(1, 2);

 cout << "\nTest1 equals " << test1;
 cout << "\nTest2 equals " << test2;
 cout << "\nTest3 equals " << test3 << endl;
 cout << ( test2 + test3 ) << endl;
 cout << ( test2 - test3 ) << endl;
 cout << ( test2 * test3 ) << endl;
 cout << ( test2 / test3 ) << endl;
 cout << boolalpha;
 cout << (test3 < test2) << endl;
 cout <<  (test2 == test3) << endl;
 system ("pause");
 return 0;
}

1
2
3
4
5
6
7
8
9
10
11
bool operator<(const Rational& rhs, const Rational& lhs)
{
    if (rhs.num < lhs.num && rhs.den < lhs.den);
    {
    return (1);
    }
    else (!(rhs.num < lhs.num) && !(rhs.den < lhs.den));
    {
    return (0);
}
}
closed account (Dy7SLyTq)
you have a semi colon at the end of your if and else
thank you its always some small error that i completely fail to see :)
closed account (Dy7SLyTq)
no problem. same thing happened to me a long time ago except it was a while loop. it was console and had a whole map on the screen. the function that had the while loop would clear the screen and then make sure the move is valid. i would run it and for some reason just see a blank screen. had to stare at it for 10 mins before i found the while(...);
if (rhs.num < lhs.num && rhs.den < lhs.den);
The above line should not end with a semicolon.

Line below should not end with a semicolon.
else (!(rhs.num < lhs.num) && !(rhs.den < lhs.den));
You can't follow an else with a condition, it would need to be else if to do that. However, you don't need the condition at all. Just a plain else would do.

In fact, because there is a return at line 5, all you would need after that is a simple return false; to satisfy the remaining cases. without any additional code at all.

The semicolon. The statements that are executed when the if evaluates true are: ;

Return true or false; the use of numbers is not as clear.

There is no if following the else.


Rethink your logic. What if neither condition is true? For example, 1/3 < 2/3.
awesome thank you that makes it a lot easier to understand. I now see why the rational number class is such a headache problem
Topic archived. No new replies allowed.