C ++“没有用于初始化的匹配构造函数”编译器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8101489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ "No matching constructor for initialization of" compiler error
提问by Xavier
I have a class which I try to initialize but get the error "No matching constructor for initialization of 'TextureCoordinates'";
我有一个类,我尝试初始化但收到错误“没有匹配的构造函数用于初始化 '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?
任何想法我做错了什么?
回答by celtschk
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.
您的构造函数通过非常量引用获取参数,但您将临时对象 ( Dimensions2D(width, height)
)传递给它。临时对象,即使是非常量的,也不会绑定到非常量引用。
Solution, make your constructor take const references (it shouldn't modify the passed objects anyway):
解决方案,让你的构造函数接受 const 引用(它不应该修改传递的对象):
TextureCoordinates(Point2D const& origin, Dimensions2D const& dim);
回答by jpalecek
TextureCoordinates result(point, Dimensions2D(width, height))
The second parameter is an rvalue that cannot be bound to lvalue reference the constructor expects:
第二个参数是一个不能绑定到构造函数期望的左值引用的右值:
TextureCoordinates(Point2D& origin, Dimensions2D& dim);
You can fix it by changing the signature of the constructor to
您可以通过将构造函数的签名更改为
TextureCoordinates(Point2D& origin, const Dimensions2D& dim);
TextureCoordinates(Point2D& origin, Dimensions2D&& dim); // alternative for c++11
(if you can) or making the parameter a variable
(如果可以)或使参数成为变量
Dimension2D dim=Dimensions2D(width, height);
TextureCoordinates result(point, dim)
回答by neagoegab
Declare Dimensions2d outside.
在外面声明 Dimensions2d。
Dimension2d d(width, height);
TextureCoordinates result(point, d);
回答by bhuwansahni
Temporary variables cannot be passed as a reference in C++ because then you can change the value of a temporary object in the function that is soon going to disappear!! No such problem exists for reference to constants..... So your function definition should be like
临时变量不能作为 C++ 中的引用传递,因为这样您就可以在即将消失的函数中更改临时对象的值!!引用常量不存在这样的问题......所以你的函数定义应该像
TextureCoordinates(Point2D const& origin, Dimensions2D const& dim);
回答by Hicham
did you write the implementation of :
您是否编写了以下实现:
TextureCoordinates(Point2D& origin, Dimensions2D& dim);