Working with classes question

I am working on a project that has us working with classes and in the project we are using a .cpp .hpp and a main method.cpp. I have it working to store the input from the main file and output them. where I am having trouble is when I incorporate the the distanceTo into the main and I get (no matching function for call to ‘Point::distanceTo(Point&)’ double dist = p1.distanceTo(p2);) Further how to I take the X's and the Y's from p1 and p2 and get the hypotenuse? I figure I would need to take the absolute value of both x and y add them together then use that good old Pythagorean theorem to get the length of the hypotenuse.

Point.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "Point.hpp"
#include <iostream>
#include <cmath>
using std::abs;


Point::Point(double x, double y)
{
   setXCoord(x);
   setYCoord(y);
}


double Point::setXCoord(double x)
{
   XPoint = x;
}
double Point::setYCoord(double y)
{
   YPoint = y;
}



double Point::getXCoord()
{
   return XPoint;
}

double Point::getYCoord()
{
   return YPoint;
}

double Point::distanceTo(double hyp)
{
   
  return sqrt(pow((XPoint - XPoint), 2) + pow((YPoint - YPoint), 2));
 
}


Point.hpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#ifndef Point_HPP
#define Point_HPP


class Point
{
   private:
      double XPoint;
      double YPoint;

   public:

      Point(double x, double y);

      double setXCoord(double x);

      double setYCoord(double y);

      double getXCoord();

      double getYCoord();

      double distanceTo(double &hyp);
      
};

#endif




Pointmain.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
#include "Point.hpp"

using namespace std;


int main ()
{

   Point p1 = Point(-1.5, 0);
   Point p2 = Point(1.5, 4);

   cout << p1.getXCoord() << endl;
   cout << p2.getXCoord() << endl;
   cout << p1.getYCoord() << endl;
   cout << p2.getYCoord() << endl << endl;
   double dist = p1.distanceTo(p2);
   cout << dist << endl;


   return 0;
}
Last edited on
What is the function distanceTo supposed to be doing? Is it supposed to be finding the distance between 2 Points? It seems as if you are trying to find the hypotenuse, which doesn't make sense, as the hypotenuse does not exist if there are only 2 Points (i.e. 1 line).
yes I am trying to take input from the main file as 2 sets of points and use them to find the hypotenuse I it should have been using the distance formula in the .cpp file thought I changed it. the end result is the same error though.
yes I am trying to take input from the main file as 2 sets of points and use them to find the hypotenuse


This doesn't make sense. 2 points give you a SINGLE line. You need at least 2 sides of a triangle to find the hypotenuse.
Last edited on
2 sets of points.
Hi,

Your function needs to take 2 points as arguments. Then you need to subtract P1.x from P2.x and subtract P1.y from P2.y, then do the Pythagoras.

Rather than use the std::pow function to square a number, it's more efficient to just multiply them.

Good luck!!
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <cmath>

class Point
{
   private:
      double XPoint;
      double YPoint;

   public:

      Point(double, double);
      
      void setXCoord(double); // <--
      void setYCoord(double); // <--

      double getXCoord(); // <--
      double getYCoord(); // <--

      double distanceTo(Point);
      
};

Point::Point(double x, double y)
{
   setXCoord(x);
   setYCoord(y);
}

void Point::setXCoord(double x)
{
   XPoint = x;
}

void Point::setYCoord(double y)
{
   YPoint = y;
}

double Point::getXCoord()
{
   return XPoint;
}

double Point::getYCoord()
{
   return YPoint;
}

double Point::distanceTo(Point aPoint) // <--
{
  double dx = XPoint - aPoint.XPoint; // <--
  double dy = YPoint - aPoint.YPoint; // <--
  
  return sqrt(dx*dx + dy*dy);
}

int main ()
{
    Point p1 = Point(-1, 2);
    Point p2 = Point(4, 14);
    
    std::cout << p1.getXCoord() << std::endl;
    std::cout << p2.getXCoord() << std::endl;
    std::cout << p1.getYCoord() << std::endl;
    std::cout << p2.getYCoord() << std::endl << std::endl;

    double dist = p1.distanceTo(p2);
    std::cout << dist << std::endl;
    
    return 0;
}
Last edited on
closed account (48T7M4Gy)
I figure I would need to take the absolute value of both x and y add them together then use that good old Pythagorean theorem to get the length of the hypotenuse


I forgot to mention that you don't need to worry about the absolute values because you are squaring the x difference and y difference so whatever they are and whatever the order of p1 and p2, ie p1->p2 or p2->p1 the sum of the squares of ie the differences will always be positive. ie the scalar distance is always positive. ( The direction vector doesn't use absolute values either.)
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/194484/
Topic archived. No new replies allowed.