I have a class which I try to initialize but get the error “No matching constructor for initialization of ‘TextureCoordinates'”;
Class which I’m trying to initialize:
class TextureCoordinates
{
public:
TextureCoordinates(){};
TextureCoordinates(Point2D& origin, Dimensions2D& dim);
Point2D getOrigin() const {return origin;};
Dimensions2D getDim() const {return dim;};
private:
Point2D origin;
Dimensions2D dim;
};
Line with compiler error:
TextureCoordinates result(point, Dimensions2D(width, height));
Definition of constructor:
TextureCoordinates::TextureCoordinates(Point2D& origin, Dimensions2D& dim):
origin(origin), dim(dim) {}
Any ideas what I’m doing wrong?
Your constructor takes the arguments by non-const reference, but you pass a temporary object (
Dimensions2D(width, height)) to it. Temporaries, even non-const ones, do not bind to non-const references.Solution, make your constructor take const references (it shouldn’t modify the passed objects anyway):