Program error when i try to construct a triangle with two point objects

So my program is supposed to make two points(each with an x and y value that is inputed by the user), find a third point that would make a right triangle and print out the value of the third point. I finished it but in the main when it tries to construct a triangle(with two references to points) the debugger says "no matching function for call to 'Point::Point(). Please tell me if im doing anything wrong. I don't know what to do to fix it.

Code:

struct Point
{
public:
Point(int x,int y);
~Point ();
int getX() const;
int getY() const;

private:
int xCord;
int yCord;

};


struct Triangle
{
public:
Triangle(const Point& p1, const Point& p2);
~Triangle();
Point getFirstPoint() const;
Point getSecondPoint() const;
Point getThirdPoint() const;

private:
Point pointOne;
Point pointTwo;
Point pointThree;
Point findThirdPoint();

};

int main()
{
int xCord;
int yCord;

// user inputs values for point 1

Point one = Point(xCord, yCord);

// user inputs values for point 2

Point two = Point(xCord, yCord);
const Point &pOne = one;
const Point &pTwo = two;

// This next line is the call to a constructor that throws an error

Triangle tr = Triangle(pOne,pTwo);

// print out the third point of the triangle

return 0;
}

// Triangle constructor
Triangle::Triangle(const Point& p1, const Point &p2)
{
pointOne = p1;
pointTwo = p2;
pointThree = findThirdPoint();
}


//point constructor
Point::Point(int x,int y)
{
xCord = x;
yCord = y;
}


Thanks in advance
1
2
3
4
5
6
Triangle::Triangle(const Point& p1, const Point &p2)
{
    pointOne = p1;
    pointTwo = p2;
    pointThree = findThirdPoint();
}


Should be:

1
2
3
4
Triangle::Triangle(const Point& p1, const Point &p2)
    : pointOne(p1), pointTwo(p2), pointThree(findThirdPoint())
{
}


Otherwise your code is equivalent to:
1
2
3
4
5
6
7
Triangle::Triangle(const Point& p1, const Point &p2)
    : pointOne(), pointTwo(), pointThree()
{
    pointOne = p1;
    pointTwo = p2;
    pointThree = findThirdPoint();
}


And since there is no default constructor for Point defined, the compiler can't generate that code. You should prefer using an initialization list in constructors as opposed to assignment in the body of the function.
Topic archived. No new replies allowed.