&引用;错误:没有匹配的函数用于调用"; 我在科德帕德,我正试图用C++来增强我的技能。我以前很少使用模板,所以我试着研究如何使用它们。下面的代码就是结果,不幸的是,它不起作用。我确实试图寻找问题的解决方案,但由于我没有太多使用模板的经验,我无法将我的问题与其他问题联系起来。所以,我决定寻求帮助 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; } 模板 类向量2{ 公众: A x,y; 向量2(一个xp,一个yp){ 这->x=xp; 这->y=yp; } }; 模板 类射线广播{ 公众: 光线广播(B角度,矢量2原点,矢量2点1,矢量2点2){ 这个->角度=角度; 此->点1=点1; 此->点2=点2; } 私人: B兰格尔; 向量2点1,点2; }; int main(){ 射线广播射线(45,Vector2(0.0,0.0),Vector2(-10.0,-3.0),Vector2(5.0,7.0)); 返回0; }

&引用;错误:没有匹配的函数用于调用"; 我在科德帕德,我正试图用C++来增强我的技能。我以前很少使用模板,所以我试着研究如何使用它们。下面的代码就是结果,不幸的是,它不起作用。我确实试图寻找问题的解决方案,但由于我没有太多使用模板的经验,我无法将我的问题与其他问题联系起来。所以,我决定寻求帮助 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; } 模板 类向量2{ 公众: A x,y; 向量2(一个xp,一个yp){ 这->x=xp; 这->y=yp; } }; 模板 类射线广播{ 公众: 光线广播(B角度,矢量2原点,矢量2点1,矢量2点2){ 这个->角度=角度; 此->点1=点1; 此->点2=点2; } 私人: B兰格尔; 向量2点1,点2; }; int main(){ 射线广播射线(45,Vector2(0.0,0.0),Vector2(-10.0,-3.0),Vector2(5.0,7.0)); 返回0; },c++,templates,constructor,C++,Templates,Constructor,以下是输出: 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<

以下是输出:

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.
t.cpp:在构造函数“rayToCast::rayToCast(B,Vector2,Vector2,Vector2)[其中B=short int,A=float]”中:
t、 cpp:26:从此处实例化
第14行:错误:调用“Vector2::Vector2()”时没有匹配的函数
编译因-Wfatal错误而终止。

非常感谢您的帮助。

当您不声明构造函数时,会自动生成默认构造函数(默认构造函数和复制构造函数)

当您声明一个构造函数时,您将不再获得自动生成的构造函数(因此,如果需要,您必须手动定义其他构造函数)


因为您定义了
Vector2(xp,yp)
,编译器不再自动为您生成
Vector2()
,您必须定义
Vector2()
如果您想使用它,请自己动手。

rayToCast构造函数通过调用
Vector2
的默认构造函数尝试初始化
point1
point2
。但它没有

必须为vector类提供默认构造函数,或者显式初始化
rayToCast
的成员。一种方法是这样做:

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)
   : RAngle(angle), point1(point1), point2(point2)
{ }
rayToCast(B角度,矢量2原点,矢量2点1,矢量2点2)
:角度、点1(点1)、点2(点2)
{ }

错误与模板无关

您的
Vector2
类没有默认构造函数,但您希望在
rayToCast
的构造函数中使用默认构造函数创建它


rayToCast
的构造函数中使用成员初始值设定项列表,或在
Vector2
中创建默认构造函数您的代码中有两个问题:

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2){
    this->RAngle = angle;
    this->Point1 = point1;
    this->Point2 = point2;
}
Vector2没有默认构造函数,将Vector2传递给
rayToCast
构造函数时将调用默认构造函数

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)
此外,如果您有输入错误,应该是Point1、Point2,而不是Point1/Point2

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;    // capital P
    Vector2<A> Point2;    // capital P
};
类光线广播{
公众:
光线广播(B角度,矢量2原点,矢量2点1,矢量2点2){
这个->角度=角度;
此->点1=点1;
此->点2=点2;
}
私人:
B兰格尔;
向量2点1;//大写字母P
向量2点2;//大写字母P
};

正如其他人已经指出的,错误在于缺少默认构造函数。使用Bo Persson建议的语法

另一个错误是变量名区分大小写,例如,在
rayToCast
的构造函数中,您编写:

this->Point1 = point1;
但是,您将类属性命名为
point1
。请注意,它不同于:

this->point1 = point1;

将这部分代码:

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2){
    this->RAngle = angle;
    this->Point1 = point1;
    this->Point2 = point2;
}

我还建议整理一下变量和参数名,因为它们有点让我困惑(这里有大写字母,不在那里,哪里有大写字母,到处都有大写字母,哪里有大写字母!)。

@H2CO3你能支持这一说法吗@学习使用成员初始值设定项列表:问题在于缺少默认构造函数。大写是完全不相关的,也不一定是个问题。主要问题是默认构造函数,但当我看到它时,为什么不指出呢?因此,它是:p遗漏了它们在构造函数中被错误使用的地方。抱歉,每天我都学到了几乎没有什么关于C++的知识。谢谢
rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)
   : RAngle(angle)
   , point1(point1)
   , point2(point2)
{}