Well hello there people of Bay12, I'm trying to overload an operator in C++ but I just can't seem to do it. Seeing how it's pretty basic stuff, I thought someone on here might be able to help me.
I'm trying to overload the operation so that if I use Point1>Point2, it returns weither Point1.x+Point1.y is larger then Point2.x+Point2.y. Pretty useless but atleast I'd be able to see how overloading works.
This is what I have now:
class Point {
private:
int x;
int y;
public:
//Point operator>(Point& c);
Point operator>(const Point&) const;
Point() { // default constructor
x = 0;
y = 0;
}
bool Point::operator>(const Point& c) const{
if ((x+y)>(c.getX()+c.getY())){
return true;
}else{
return false;
}
}
int Point::getX(){
return x;
}
int getY(){
return y;
}
Point(int new_x, int new_y) { // constructor
x = new_x;
y = new_y;
}
void showLoc(){
cout <<"("<<x<<","<<y<<")\n";
}
void setX(int newX){
x=newX;
}
void setY(int newY){
y=newY;
}
void setLoc(int newX,int newY){
x=newX;
y=newY;
}
int CalculateDistance(Point two){
int distance;
distance=(int)sqrt(pow(double(getX()-two.getX()),2)+pow(double(getY()-two.getY()),2));
return distance;
}
};
The important part is:
Point operator>(const Point&) const;
Point() { // default constructor
x = 0;
y = 0;
}
bool Point::operator>(const Point& c) const{
if ((x+y)>(c.getX()+c.getY())){
return true;
}else{
return false;
}
}
Now, the operator> function says ''Unable to overload function distinguished with return type alone'' but I have no idea what I can do. Anyone know what I'm doing wrong?
ps: I recall some other programming help request threads, maybe we can turn this one into a ''Ask all your programming questions here!'' thread if people are interested.