I just finished creating a “Point” class and was working on a “Line” class that has two Point objects (Point startpoint and Point endpoint) as data members. I created a constructor in my Line class that accepts two Point objects as arguments and had originally created it as follows:
Line(const Point& p1, const Point& p2)
{
startpoint = p1;
endpoint = p2;
}
Everything was fine, but then I decided I would use a member initialization list instead of assigning the members to p1 and p2 in the body, just because… But when I changed it to:
Line(const Point& p1, const Point& p2): startpoint(p1), endpoint(p2)
{
}
I get an error saying “no instance of constructor “Point::Point” matches the argument list” and didn’t understand what this meant exactly.
Why doesn’t the member initialization list work here?
Thanks.
edit: Sorry, I didn’t know if the details of my point class was relevant or not:
// Point.h
class Point
{
private:
double x;
double y;
public:
Point();
Point(Point& p);
Point(double x1, double y1);
~Point();
double X() const;
double Y() const;
void X(double newx);
void Y(double newy);
};
// Point.cpp
#include "Point.h"
Point::Point(): x(0), y(0)
{
}
Point::Point(Point& p)
{
x = p.x;
y = p.y;
}
Point::Point(double x1, double y1): x(x1), y(y1)
{
}
Point::~Point()
{
}
double Point::X() const
{
return x;
}
double Point::Y() const
{
return y;
}
void Point::X(double newx)
{
x = newx;
}
void Point::Y(double newy)
{
y = newy;
}
The member initialization depends on
Pointhaving a public copy constructor, since you explicitly callingPoint::Point(const Point&).If that doesn’t exist or is not accessible, you can’t call it. If you can’t call it, it isn’t because the initialization list doesn’t work, though.
Presumably
Pointhas an accessible assignment operator, if the first version works.Just to confirm, now you’ve pasted the source for
Point, the copy constructor should look like:(you might as well get comfortable using the initializer list).
The key change is the argument must be a
constreference: this prohibits accidentally damaging the source object when copying.