"错误:没有匹配的函数来调用"

aws*_*r27 4 c++ templates constructor

我在键盘上,我正在尝试使用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;
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

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.
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

Bo *_*son 6

rayToCast构造函数试图初始化point1point2通过调用Vector2的默认构造函数.但它没有一个.

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

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)
   : RAngle(angle), point1(point1), point2(point2)
{ }
Run Code Online (Sandbox Code Playgroud)