Functions and Classes question.

So this is a build off another questions that I sorted out. I have a Point.cpp , Point.hpp LineSegment.cpp and LineSegment.hpp

The LineSegment.cpp should read from a function in the Point.cpp. I think I have that sorted out but I am getting an error

1
2
  Pointmain.cpp:12:26: error: no matching function for call to ‘LineSegment::LineSegment(Point&, Point&)’
    LineSegment ls1(p1, p2);


point.cpp
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
#include <iostream>
#include <cmath>
#include "Point.hpp"

using namespace std;

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

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

double Point::getXCoord()
{
   return XCoord;
}
double Point::getYCoord()
{
   return YCoord;
}

double Point::distanceTo(const Point& p)
{
 
   return sqrt(pow((p.XCoord - XCoord), 2) + pow((p.YCoord - YCoord), 2));

}


Point.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef POINT_HPP
#define POINT_HPP

class Point
{
   private:
      double XCoord;
      double YCoord;

   public:
      Point();
      Point(double, double);
      void setXCoord (double);
      void setYCoord (double);
      double getXCoord();
      double getYCoord();
      double distanceTo(const Point& p);

};
#endif 


LineSegment.cpp
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
#include <iostream>
#include <cmath>
#include "LineSegment.hpp"

using namespace std;

LineSegment::LineSegment(double x, double y)
{
   setEnd1(x);
   setEnd2(y);
}
void LineSegment::setEnd1(double x)
{ 
   end1 = x;
}

void LineSegment::setEnd2(double y) 
{
   end2 = y;
}

double LineSegment::getEnd1()
{
   return end1;
}
double LineSegment::getEnd2()
{
   return end2;
}

double length(Point& p1, Point& p2) 
{
   return p1.distanceTo(p2);
}

double LineSegment::slope(const LineSegment& l)
{
   return (l.end2 - end2) / (l.end1 - end1);
}


LineSegment.hpp
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
#ifndef LineSegment_HPP
#define LineSegment_HPP
#include "Point.hpp"

class LineSegment
{
   private:
      double end1;
      double end2;

   public:

      LineSegment(double, double);

      void setEnd1(double);

      void setEnd2(double);

      double getEnd1();

      double getEnd2();

      double length(Point& p1, Point& p2); 

      double slope(const LineSegment& l);

#endif
};


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

using namespace std;

int main()
{
   Point p1( -1.5, 0);
   Point p2( 1.5, 4);

   LineSegment ls1(p1, p2);
   double length = ls1.length();
   double slope = ls1.slope();

  double dist = p1.distanceTo( p2 );
   cout << dist << endl;
   return 0;
}
On line 13 of LineSegment.hpp we see that the constructor for LineSegment takes two arguments of type double.

On line 12 of PointMain.cpp we see that you're trying to create a LineSegment with two arguments of type Point.


So, there is no constructor that matches the arguments you've fed to it on line 12 of PointMain.cpp.

[Edit: Conversion stuff was backwards. There is no way to convert from type Point to type double so there is no way for the compiler to convert the arguments to an acceptable type.]
Last edited on
so I build a constructor that takes doubles and passes it Point?


so then I need to rebuild them all to type Point to take in the data from the Point.cpp?
Last edited on
Obviously, the Point type is meant to describe a location in 2d space. Your LineSegment, on the other hand, could only describe a segment in 1d space (if that's even possible.) Yes. LineSegment should be implemented using the Point type.
Last edited on
I trying changing it to Point Type.

 
LineSegment(Point&, Point&);

and still did not resolve the issue
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/194409/
closed account (48T7M4Gy)
A line segment is defined between two Points p1 (x1, y1) and p2(x2,y2)

The length of the line segment is as you have calculated before by Pythagoras theorem without regard to sign.

L = sqrt( dx^2 + dy^2), whichever way you decide to calculate it.

However for calculating the slope the direction is important as against just calculating the scalar distance between two points. So dx = x2 - x1, dy = y2 - y1 for L1->2

The slope of the line is dy/dx for the tangent the normal way of describing the slope, or dy/L for sine. Both cases are 'direction sensitive'.

Last edited on
Topic archived. No new replies allowed.