C++ 错误:没有构造函数的实例与参数列表匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19479200/
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
Error: No instance of constructor matches the argument list
提问by Daniel Frey
As part of an assignment we have been asked to create a Vector3D class which uses memory allocated on the Heap. I have a Vector3DHeap class with the following constructor.
作为作业的一部分,我们被要求创建一个 Vector3D 类,该类使用在堆上分配的内存。我有一个带有以下构造函数的 Vector3DHeap 类。
Vector3DHeap::Vector3DHeap(float& x, float& y, float& z)
{
this->x = &x;
this->y = &y;
this->z = &z;
}
If I want to get a unit vector, I was expecting the be able to do the following. This gives the error message "No instance of constructor matches the argument list, argument types are (float, float, float).
如果我想获得单位向量,我希望能够执行以下操作。这给出了错误消息“没有构造函数的实例与参数列表匹配,参数类型为(浮点数、浮点数、浮点数)。
Vector3DHeap* Vector3DHeap::getUnitVector()
{
float m = *getMagnitude();
return new Vector3DHeap((*x / m), (*y / m), (*z / m));
}
The compiler is happy if I define three float variables, a, b and c and pass these to the constructor. What is wrong with the code above?
如果我定义三个浮点变量 a、b 和 c 并将它们传递给构造函数,编译器会很高兴。上面的代码有什么问题?
Vector3DHeap* Vector3DHeap::getUnitVector()
{
float m = *getMagnitude();
float a, b, c;
a = *x / m;
b = *y / m;
c = *z / m;
return new Vector3DHeap(a, b, c);
}
Many thanks, George
非常感谢,乔治
回答by Daniel Frey
Your problem with the first version is that your compiler is trying to prevent a bug.
您对第一个版本的问题是您的编译器正试图防止出现错误。
Your problem with the secondversion is that you outsmarted your compiler and successfully managed to create a bug.
您对第二个版本的问题是您比编译器更聪明并成功地创建了一个错误。
Given your constructor, you want to store pointers to the float
values that are passed by reference. Since your second version now calls the constructor with references to the local variables float a, b, c;
, you created an instance of Vector3DHeap
which references them. But as soon as getUnitVector
returns, those variables no longer exist and the references stored in Vector3DHeap
became dangling references.
给定您的构造函数,您希望存储指向float
通过引用传递的值的指针。由于您的第二个版本现在使用对局部变量的引用来调用构造函数float a, b, c;
,因此您创建了一个Vector3DHeap
引用它们的实例。但是一旦getUnitVector
返回,这些变量就不再存在,存储在其中的引用Vector3DHeap
就变成了悬空引用。
The solution is not to store pointers inside Vector3DHeap
or to create copies of the parameters:
解决方案不是在内部存储指针Vector3DHeap
或创建参数的副本:
Vector3DHeap::Vector3DHeap(float x, float y, float z)
{
this->x = new float(x);
this->y = new float(y);
this->z = new float(z);
}
Make sure that you properly delete the stored floats, though.
不过,请确保您正确删除了存储的浮点数。
回答by Dietmar Kühl
It is good that the compiler stopped you from binding a reference to a temporary because otherwise you would have ended up with an object pointing to already destroyed objects: The expressions *x / m
and similar each yield a temporary float
object which will disappear at the end of the expression. Trying to bind a temporary to a non-const
reference will fail.
编译器阻止您将引用绑定到临时对象是件好事,否则您最终会得到一个指向已销毁对象的对象:表达式*x / m
和类似的每个都会产生一个临时float
对象,该对象将在表达式结束时消失。尝试将临时对象绑定到非const
引用将失败。
However, I doubt that you really want to do any of that: you shouldn't use pointers unless you really know that you need to use pointers! Your constructor should probably rather look like this:
但是,我怀疑您是否真的想执行任何操作:除非您真的知道需要使用指针,否则不应使用指针!你的构造函数应该看起来像这样:
Vector3DHeap::Vector3DHeap(float x, float y, float z)
: x(x), y(y), z(z) {
}
where the members are, of course, also of type float
. getMagnitude()
should return a float
, too. ... as should getUnitVector()
return a Vector3DHeap
rather than a pointer to it!
当然,成员的类型也是float
。getMagnitude()
也应该返回 a float
。... 应该getUnitVector()
返回一个Vector3DHeap
而不是指向它的指针!
回答by Oswald
(*x / m)
is a temporary object.Vector3DHeap(float& x, float& y, float& z)
requires a non-const reference as first parameter.
(*x / m)
是一个临时对象。Vector3DHeap(float& x, float& y, float& z)
需要一个非常量引用作为第一个参数。
You can't pass a temporary object to a function that expects a non-const reference. See https://stackoverflow.com/questions/13826897#13827042for details why C++ does not want to allow this.
您不能将临时对象传递给需要非常量引用的函数。请参阅https://stackoverflow.com/questions/13826897#13827042了解 C++ 不允许这样做的详细信息。