“错误:没有匹配的调用函数”。 [英] "error: no matching function for call to"
问题描述
我当时在键盘上,我试图使用C ++来提高自己的技能。我以前从未使用过模板,因此尝试研究如何使用它们。下面的代码是结果,但不幸的是,它不起作用。我确实尝试寻找问题的解决方案,但是由于我在使用模板方面没有太多经验,因此无法在问题和其他问题之间建立任何联系。因此,我决定寻求帮助。
I was on codepad and I was trying to build up my skills using C++. I'd never used templates much before, so I tried looking into how to use them. The code below is the result, and unfortunately, it does not work. I did try to search for solutions to my problem, but since I don't have much experience using templates, I couldn't make any connections between my problem and other problems. So, I decided to ask for help.
template <class A>
class Vector2 {
public:
A x,y;
Vector2(A xp, A yp){
this->x = xp;
this->y = yp;
}
};
template <class B, class A>
class rayToCast {
public:
rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2){
this->RAngle = angle;
this->Point1 = point1;
this->Point2 = point2;
}
private:
B RAngle;
Vector2<A> point1,point2;
};
int main(){
rayToCast<short int, float> ray(45, Vector2<float>(0.0, 0.0), Vector2<float>(-10.0, -3.0), Vector2<float>(5.0, 7.0));
return 0;
}
这是输出:
t.cpp: In constructor 'rayToCast<B, A>::rayToCast(B, Vector2<A>, Vector2<A>, Vector2<A>) [with B = short int, A = float]':
t.cpp:26: instantiated from here
Line 14: error: no matching function for call to 'Vector2<float>::Vector2()'
compilation terminated due to -Wfatal-errors.
我们将不胜感激。
推荐答案
rayToCast
构造函数尝试初始化 point1
和 point2
,方法是调用 Vector2
的默认构造函数。
The rayToCast
constructor tries to initialize point1
and point2
by calling Vector2
's default constructor. But it doesn't have one.
您必须为vector类提供默认的构造函数,或者显式初始化 rayToCast
。一种方法是这样进行:
You either have to provide a default constructor for the vector class, or explicitly initialize the members of rayToCast
. One way is to do like this:
rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)
: RAngle(angle), point1(point1), point2(point2)
{ }
这篇关于“错误:没有匹配的调用函数”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!