Operator overload

How come is this incorrect?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class car {
public:
    car(int);
    int HorsePower;
     int getHorsepower();
    car operator == (const car,car rhs){
        if(this->lhs.getHorsepower() < rhs.getHorsepower())
        {
            cout << "Too strong" << endl;
        }
        
        else
            cout << "too weak" << endl;
    }

};

int car::getHorsepower()
{
    return HorsePower;
}
car::car(int c)
{
    HorsePower = c;
}
A comparison operator should return a bool. Also, the syntax is a bit messed up there.

Note, the example is a bit simple but it shows how it works.
http://ideone.com/nASIce
Last edited on
The whole operator== is wrong.

Syntax:
"lhs" is never defined
member operator== should have one parameter

operator == should return something, and that something should be a bool not a car

consider this example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    bool operator == (const car &rhs)
    {
        if(HorsePower < rhs.HorsePower)
        {
            cout << "Too strong" << endl;
            return false;
        }
        else if (HorsePower > rhs.HorsePower)
        {
        	cout << "too weak" << endl;
        	return false;
    	}
    	
    	return true;
    }
I'd also like to point out that by convention, overloading operator== shouldn't do anything besides returning true or false. You should really write another function besides the operator overload to do the comparison and print the result.
Topic archived. No new replies allowed.