-1

I am creating a class to print a point, compare two points to see if they are equal, and to find the distance between two points using separate methods for each. The method for finding the distance between two points is giving me a type error and I don't know why.

    #include <iostream>
using namespace std;

class Point 
{//in C++ stuff is private by default

public:
    Point() { double x = 0; double y = 0; }
Point (double a, double b) { x = a; y = b; }

void print() { cout << "(" << x << "," << y << ")\n" << endl; }

double getX() { return x; };
double getY() { return y; };

bool compare(Point other) { return (x == other.x && y == other.y); }

void setX(double a) 
{if (a >= 0)
    {x = a;}};

void setY(double b)
{if (b >= 0)
    {y = b;}};

double distance(Point point1, Point point2)
{
return(sqrt (pow (point1.getX-point2.getX,2) + pow(point1.getY-point2.getY,2)));
};

private:
    double x, y;
};


bool Compare(Point a, Point b) { return (a.getX() == b.getX()) && (b.getY() == a.getY()); }


int main()
{
    Point p1(5,1);
    Point p2;

    p2.setX(2);
    p2.setY(5);

    p1.print();
    p2.print(); 

    p1.getX();
    p1.getY();

    p2.getX();
    p2.getY();

    p1.setX(3.5);
    p1.setY(9);

    p1.print();


        p1.compare(p2);
    //or p2.equals(p1);
        distance(p1, p2); 

        cout << "This distance b/w p1 & p2 is:" << distance (p2, p1) << endl;

}
rddead
  • 103
  • 10
  • 3
    `getX` and `getY` are *functions*, not members. You need to use function-call syntax, *e.g.*, `getX()` – Cody Gray - on strike Mar 02 '16 at 15:19
  • 2
    Don't paraphrase the errors that you are getting. Copy-paste them exactly as they appear. – Algirdas Preidžius Mar 02 '16 at 15:19
  • I am getting the following: Error (active) no instance of function template "std::distance" matches the argument list - identifier cout is unidentified – rddead Mar 02 '16 at 15:35
  • You made `distance` a member function. Either make it a free function with two parameters, or a member with one parameter (like `compare`). – molbdnilo Mar 02 '16 at 15:40
  • It is neater to initialize members in the constructor, rather than assign them: So `Point(double a, double b) : x(a), y(b) {}`. Also, you should make getX, and getY const members: `double getX() const { return x; }`. compare should also be const, and you want to think about the wisdom of comparing doubles for exact equality. It can work, but it can also cause huge amounts of paint. – Martin Bonner supports Monica Mar 02 '16 at 15:48
  • Final point, you ignore arguments to setX and setY which are negative, but you allow negative values in the constructor. (Personally, I would allow them throughout). – Martin Bonner supports Monica Mar 02 '16 at 15:49
  • @MartinBonner the points you mention (negative values, compare) are required in the instructions for this assignment. Initializing in the constructor is something I will do though. Less lines = better. – rddead Mar 02 '16 at 15:52
  • paint?? That should be "huge amounts of pain". – Martin Bonner supports Monica Mar 02 '16 at 20:18

3 Answers3

4

You have to call the methods getX and getY by adding () after each name:

return(sqrt(pow(point1.getX()-point2.getX(),2) + pow(point1.getY()-point2.getY(),2)));

Otherwise you will be subtracting pointers to functions, which isn't allowed.

owacoder
  • 4,815
  • 20
  • 47
1
#include <iostream>
#include <math.h>
using namespace std;

class Point 
{//in C++ stuff is private by default

public:
    Point() { double x = 0; double y = 0; }
Point (double a, double b) { x = a; y = b; }

void print() { cout << "(" << x << "," << y << ")\n" << endl; }

double getX() { return x; };
double getY() { return y; };

bool compare(Point other) { return (x == other.x && y == other.y); }

void setX(double a) 
{if (a >= 0)
    {x = a;}};

void setY(double b)
{if (b >= 0)
    {y = b;}};

static double distance1(Point point1, Point point2)
{
return(sqrt (pow (point1.getX()-point2.getX(),2) + pow(point1.getY()-point2.getY(),2)));
};

private:
    double x, y;
};


bool Compare(Point a, Point b) { return (a.getX() == b.getX()) && (b.getY() == a.getY()); }


int main()
{
    Point p1(5,1);
    Point p2;

    p2.setX(2);
    p2.setY(5);

    p1.print();
    p2.print(); 

    p1.getX();
    p1.getY();

    p2.getX();
    p2.getY();

    p1.setX(3.5);
    p1.setY(9);

    p1.print();


        p1.compare(p2);
    //or p2.equals(p1);
        //distance(p1, p2); 

        cout << "This distance b/w p1 & p2 is:" << Point::distance1 (p2, p1) << endl;

}
0

Replace distance function from class. In your case std::distance is called. And try do not use using namespace std; in your code at all.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
malchemist
  • 434
  • 2
  • 13
  • 1
    A better solution would be to remove `using namespace std;` – Cody Gray - on strike Mar 02 '16 at 15:23
  • Is there an explanation for why using namespace std is wrong? I am aware of the std:: syntax but it's not used where I am taught. What makes one better than the other? – rddead Mar 02 '16 at 15:27
  • It isn't wrong. But `using namespace std;` can lead to errors in big projects or when you use libraries(it's your case). – malchemist Mar 02 '16 at 15:31
  • 3
    @nyx You have a perfect example right here. There is a `std::distance` function, and you have defined a `distance` function of your own. That's a potential name clash, and preventing name clashes is the whole raison d'être of namespaces. It is much easier to type `std::` than dealing with these problems. See also: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Cody Gray - on strike Mar 02 '16 at 15:31