Overloading > operator

Hi!
I'm trying to overload the > (greater than) operator for my object Point. However my compiler keeps complaining that the object cannot be compared. The exact error is: invalid operands to binary expression ('Point' and 'Point')
Could you tell me what I'm doing wrong?
This is my code:

Point.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Point {
    
private:
    int x, y;

public:
    Point();
    Point(int x, int y);
    ~Point();
    inline bool operator> (Point &b) {
        return (this->distance(0, 0) > b.distance(0, 0));
    }
    
    int getX();
    int getY();
    void setX(int x);
    void setY(int Y);
    
    double distance(int x, int y);
    double distance(Point p);
};


main.cpp:
1
2
3
4
5
Point pp1(0, 9);
    Point pp2(0, 10);
    if(pp1 < pp2){
        
}


Thanks in advance!
Last edited on
1
2
3
  bool operator> (Point &b)
..
if(pp1 < pp2)

Do you notice a mismatch?

Side-notes:
Use const: bool operator> (const Point &rhs) const
You are not going to modify either object in your relational operator, so make that explicit.

The convention is to implement op< and op==. The standard library provides template functions that implement the other relational operations in terms of these two.
Whoops! You are right.. now that was really stupid of me.
I turned the < into a > in my main.cpp but my compiler seems to keep complaining.
This time I got these error, but I don't know what to do:
http://img832.imageshack.us/img832/3351/cpplinkererror.png
You don't have implementation for those three member functions. If you do have them, then they are in a compilation unit (object file) that is not included in the linking phase.
You were right.. I had to place the method in the Point.cpp file and include the .cpp file of Point to make this work. Damn I hate dirty coding :(

Thanks keskiverto!
Last edited on
-pedantic

they are in a compilation unit (object file) that is not included in the linking phase

To me, writing "compilation unit (object file)" implies that object file and compilation unit are different terms for the same thing, which they are not.

A translation unit (to give compilation unit its official name) is a source file after it has been preprocessed. That is, the contents of all the included headers have been merged in, the macros expanded, and condition code segment excluded or included as appropriate (#ifdef, etc)

The object file is the binary file which is the result of the translation (or compilation) of this unit.

Hence you can include an object file in the linking phase, but not a compilation unit.

Andy
That's nice to know Andy.
Thanks for your addition/correction!
Topic archived. No new replies allowed.