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:
virtualdouble area() = 0; // area calculator
friendbooloperator< (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
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.
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"
booloperator< (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;
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.)