0

I'm currently trying to set up a program for a Point class in C++, with three constructors, a default one that sets x and y to 0, a one-parameter constructor that takes y from first two digits (of a four digit number) and x from the last two digits, and a two-parameter constructor that takes in an input for x and y.

I have a method for finding distance and the point's quadrant which should work currently.

Now I'm told to overload the > operator to compare the distance from the origin to two Point objects being compared. Thing is, I got no clue how to write an operator overload method, so after searching up Internet tutorials, it told me to follow a format akin to bool operator > (Point const p){, which doesn't work. The compiler I'm using just either doesn't explain the error, or says that it has to do with Point needing to take two arguments.

For context, here's the full code.

#include <stdio.h>
#include <math.h> 

class Point{
    public:
        int x, y;
        
        Point()
        {
            x = 0;
            y = 0;
        }
        Point (int theX, int theY){
            x = theX;
            y = theY;
        }
        Point (int fourDigits){
            x = fourDigits % 100;
            y = fourDigits / 100;
        }
};

int findDistance(Point p){
    
    int d = sqrt(pow(p.x, 2)+pow(p.y, 2));
    return d;
}

int findQuadrant(Point p){
    int quad;
    if(p.x==0&&p.y==0){
        quad = 0;
        printf("\nOrigin\n");
    }
    else if(p.x==0&&p.y!=0){
        quad = 5;
        printf("\nX-axis\n");
    }
    else if(p.x!=0&&p.y==0){
        quad = 6;
        printf("\nY-axis\n");
    }
    else if(p.x<0&&p.y>0){
        quad = 2;
        printf("\nSecond quadrant\n");
    }else if(p.x<0&&p.y<0){
        quad = 3;
        printf("\nThird quadrant\n");
    }else if(p.x>0&&p.y<0){
        quad = 4;
        printf("\nFourth quadrant\n");
    }else{//X>0  and Y>0 assumed to be the default
        quad = 1;
        printf("\nFirst quadrant\n");
    }
    return quad;
}

bool operator > (Point const p){//this header gives an error b/c of Point needing two arguments. 
//To be frank, I don't know if this is even the right header anyhow.
    //this is where i should overload the operators. Not sure how to do it.
}

int main()
{
     //work in progress
    
    return 0;
}
benoupkva
  • 13
  • 2
  • `>` is a *binary* operator. When you call it with `a > b`, you're passing two arguments. But your proposed function only takes one argument. – Silvio Mayolo Apr 21 '21 at 03:50
  • Your `Point` class doesn't have a copy constructor, so you can't pass it by value. You could use a const reference as the parameter to `operator>` though. – Mark Ransom Apr 21 '21 at 03:51
  • 1
    I don't write much c++, but shouldn't the operator definition be within the class definition? – moreON Apr 21 '21 at 03:53
  • 2
    @MarkRansom I don't think that's right: https://godbolt.org/z/ebMn8fqEs also yes, one parameter binary operator overload definitions go inside the class definition. – Patrick Roberts Apr 21 '21 at 03:54
  • @MarkRansom so what would the header look in this case when you include the const reference? – benoupkva Apr 21 '21 at 04:06
  • @SilvioMayolo I need to write one overload method with a Point parameter, and another method with two int parameters. Both should do basically the same thing; comparing distances between Points (or coordinates). So how would that relate to this whole "argument" thing? – benoupkva Apr 21 '21 at 04:08
  • @PatrickRoberts your code doesn't show the error stated in the question, so there must be a difference that's unaccounted for. I was basing my comment on the statement "Point needing to take two arguments". – Mark Ransom Apr 21 '21 at 04:10
  • @benoupkva `bool operator > (const Point& p)` although as others have noted it needs to be a member function. That might be your whole problem right there. – Mark Ransom Apr 21 '21 at 04:12
  • @MarkRansom So how would I make it a member function? Cuz when i put that header in, I just get an unspecified error. I assume thats when I need to make a member function. – benoupkva Apr 21 '21 at 04:14
  • Tip: Use [constructor lists](https://en.cppreference.com/w/cpp/language/constructor) to clean up your code. – tadman Apr 21 '21 at 04:31
  • You need that operator if you're ever doing sorting, among other things. You might be using that inadvertently if you use a container that's sorted. – tadman Apr 21 '21 at 04:31
  • Does this answer your question? [Overloading less than operator](https://stackoverflow.com/questions/18646526/overloading-less-than-operator) – Kfir Ventura Apr 21 '21 at 04:56
  • Not to critiqe the question, but the premise in it. "Now I'm told to overload the > operator to compare the distance from the origin to two Point objects being compared.". This is a very bad design. This operator should describe a natural ordering of things. – Captain Giraffe Apr 21 '21 at 05:22

0 Answers0