Overloading a Relational Operator

I am having some difficulty in overloading a relational operator. I have a class called "closed_curve," which is a base class for some geometrical objects. I am implementing an overloaded < operator in this base class. As an example, "rectangle" is a child class of "closed_curve," and "square" is a child class of "rectangle." I have tried using the general algorithm for an overloaded relational operator with 2 arguments, but my compiler keeps throwing the following error:

../closed_curve.cpp:23: error: 'bool closed_curve::operator<(closed_curve&, closed_curve&)' must take exactly one argument
../closed_curve.cpp:23: error: no 'bool closed_curve::operator<(closed_curve&, closed_curve&)' member function declared in class 'closed_curve'

Here is my code:

"closed_curve.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef CLOSED_CURVE_H_
#define CLOSED_CURVE_H_

#include "coordinate.h"

class closed_curve
{
public:
	virtual double area() = 0; // area calculator

	friend bool operator< (closed_curve &LHS, closed_curve &RHS); // compare two areas
};

#endif 


"closed_curve.cpp"
1
2
3
4
5
6
7
#include "closed_curve.h"

// see if the area of object on LHS is less than the area of object on RHS:
bool closed_curve::operator< (closed_curve &LHS, closed_curve &RHS)
{
	return LHS.area() < RHS.area();
}


Here are two sample objects' codes:
"rectangle.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef RECTANGLE_H_
#define RECTANGLE_H_

#include "closed_curve.h"
#include "coordinate.h"

class rectangle : public closed_curve
{
public:
	rectangle(double w_holder, double h_holder, double x_holder, double y_holder); // constructor

	double width, height; // width is in the x direction, and height is in the y direction

	double area(); // method for area
};

#endif 


"rectangle.cpp"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "rectangle.h"
#include "coordinate.h"

// constructor:
rectangle::rectangle(double w, double h, double x, double y) : width(w), height(h), center(x,y)
{

}

// calculate area:
double rectangle::area()
{
	return width*height;
}


"square.h"
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef SQUARE_H_
#define SQUARE_H_

#include "rectangle.h"

class square : public rectangle
{
public:
	square(double s_holder, double x_holder, double y_holder); // constructor
};

#endif 


"square.cpp"
1
2
3
4
5
6
7
#include "square.h"

// constructor:
square::square(double s, double x, double y) : rectangle(s, s, x, y)
{

}


Any ideas why the compiler says that my overloaded < operator should be taking only 1 argument? I thought it could take 2 arguments. Thanks!
If it's a member of a class, it already has one argument implicitly: the class instance you're using to invoke the operator.

1
2
3
4
bool closed_curve::operator< (const closed_curve &RHS) const
{
	return area() < RHS.area();
}


Added the const modifier where appropriate.

I notice you have declared an operator< as friend. You should use either the stand alone friend function (which will take 2 explicit parameters) or the member function. It's not really important which, but having them both will result in ambiguity.
Last edited on
If you declare an operator function inside of a class then the function should only take 1 parameter. The class itself is implied as the left-hand side of the operator so you only have to specify the right-hand side.

If you declare the operator function outside of a class, THEN you need to specify two arguments: the left and the right.
Thank you for your responses. I had no idea that the < operator already had one implicit argument when it's a member of a class. So, here's my new code:

"closed_curve.h"
 
	bool operator< (const closed_curve &RHS) const; // compare two areas 


"closed_curve.cpp"
1
2
3
4
5
// see if the area of object on LHS is less than the area of object on RHS:
bool closed_curve::operator< (closed_curve &RHS) const
{
	return area() < RHS.area();
}


How do I call this properly in main? For example, is the following correct?:

"main.cpp"
1
2
	// compare the areas of 2 objects:
	rectangle_object < square_object;
That's how you use it.

Your definition of operator< doesn't match the declaration. The parameter in the definition should be const.

Your area functions should also be const.




Oh... right. I forgot to put const in the function definition. I'm seeing a new error, though:

../closed_curve.cpp:25: error: passing 'const closed_curve' as 'this' argument of 'virtual double closed_curve::area()' discards qualifiers

What does this error mean?

Last edited on
It means your area function isn't const like it should be.
When I append const to the ends of my area declarations and definitions (except in the base class declaration), I still get the same error message.
passing 'const closed_curve' as 'this' argument of 'virtual double closed_curve::area()' discards qualifiers


There too. You could just forgo all const qualifiers and ignore const-correctness for the time being if you're worried about the function declaration not matching your assignment requirements, but it's not a good habit to get into (and I can't imagine your instructor deducting points for it.)
Awesome. I simply fixed my virtual function to be const-correct:

 
	virtual double area() const = 0; // area calculator 


Thank you very much for all your help!

Topic archived. No new replies allowed.