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

时间:2022-08-12 18:45:38

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.

我在键盘上,我正在尝试使用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;
}

Here's the output:

这是输出:

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.

Any help is appreciated.

任何帮助表示赞赏。

6 个解决方案

#1


6  

The rayToCast constructor tries to initialize point1 and point2 by calling Vector2's default constructor. But it doesn't have one.

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

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:

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

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)
   : RAngle(angle), point1(point1), point2(point2)
{ }

#2


3  

You have two issues in your code:

您的代码中有两个问题:

Vector2 has no default constructor, default constructor will be called when pass Vector2 to rayToCast constructor.

Vector2没有默认构造函数,当将Vector2传递给rayToCast构造函数时,将调用默认构造函数。

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)

You need to add default constructor to Vector2 and initialize x,y to default value:

您需要将默认构造函数添加到Vector2并将x,y初始化为默认值:

template <class A>
class Vector2 {
public:
    A x,y;
    Vector2() : x(), y() {}  // default constructor
    Vector2(A xp, A yp){
        this->x = xp;
        this->y = yp;
    }
};

Also, you have typo, should be Point1, Point2 instead of point1/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
};

#3


3  

The error is not about the templates.

错误与模板无关。

Your Vector2 class has no default constructor, but you want to create it with a default constructor in the constructor of rayToCast.

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

Use a member initializer list in the constructor of rayToCast or create a default constructor in Vector2.

在rayToCast的构造函数中使用成员初始值设定项列表,或在Vector2中创建默认构造函数。

#4


2  

When you don't declare a constructor, you get the default constructors auto-generated for you (default constructor and copy constructor).

如果不声明构造函数,则会获得为您自动生成的默认构造函数(默认构造函数和复制构造函数)。

When you do declare a constructor, you no longer get the auto-generated constructors (so you have to manually define the others if you want them).

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

Because you defined Vector2(A xp, A yp), the compiler no longer auto-generates Vector2() for you, and you have to define Vector2() yourself if you want to use it.

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

#5


1  

Take this part of your code:

拿这部分代码:

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

To the uninitiated C++ programmer it looks like this initialises Point1 and Point2, but it does not. Initialisation is either done by default (which cannot happen here, since Vector2<A> has no default constructor), or in the member initialisation list (which you didn't use); your assignments in the constructor body are just that: after-the-fact assignments.

对于不熟悉的C ++程序员来说,它看起来像初始化Point1和Point2,但事实并非如此。初始化要么默认完成(这里不会发生,因为Vector2 没有默认构造函数),或者在成员初始化列表中(你没有使用);你在构造函数体中的赋值就是:事后赋值。

You probably have your reasons for not giving Vector<A> a default constructor, so fix rayToCast by having its constructor properly initialise those Point1/Point2 members from the constructor arguments:

您可能有理由不给Vector 一个默认构造函数,因此通过让构造函数从构造函数参数中正确初始化那些Point1 / Point2成员来修复rayToCast:

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)
   : RAngle(angle)
   , point1(point1)
   , point2(point2)
{}

I'd also recommend tidying up your variable and argument names, as they confused me a little (caps here, not there, nowhere, everywhere, where, there!).

我还建议整理你的变量和参数名称,因为他们把我弄糊涂了一下(在这里上限,不在那里,无处,在任何地方,在哪里,那里!)。

#6


0  

As other's have already pointed out, the error is that you lack a default constructor. Use the syntax that Bo Persson suggested.

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

Another error is that variable names are case-sensitive, e.g., in the constructor of rayToCast you write:

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

this->Point1 = point1;

However, you named the class attribute point1. Note that it is not the same as:

但是,您将类属性命名为point1。请注意,它不同于:

this->point1 = point1;

#1


6  

The rayToCast constructor tries to initialize point1 and point2 by calling Vector2's default constructor. But it doesn't have one.

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

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:

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

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)
   : RAngle(angle), point1(point1), point2(point2)
{ }

#2


3  

You have two issues in your code:

您的代码中有两个问题:

Vector2 has no default constructor, default constructor will be called when pass Vector2 to rayToCast constructor.

Vector2没有默认构造函数,当将Vector2传递给rayToCast构造函数时,将调用默认构造函数。

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)

You need to add default constructor to Vector2 and initialize x,y to default value:

您需要将默认构造函数添加到Vector2并将x,y初始化为默认值:

template <class A>
class Vector2 {
public:
    A x,y;
    Vector2() : x(), y() {}  // default constructor
    Vector2(A xp, A yp){
        this->x = xp;
        this->y = yp;
    }
};

Also, you have typo, should be Point1, Point2 instead of point1/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
};

#3


3  

The error is not about the templates.

错误与模板无关。

Your Vector2 class has no default constructor, but you want to create it with a default constructor in the constructor of rayToCast.

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

Use a member initializer list in the constructor of rayToCast or create a default constructor in Vector2.

在rayToCast的构造函数中使用成员初始值设定项列表,或在Vector2中创建默认构造函数。

#4


2  

When you don't declare a constructor, you get the default constructors auto-generated for you (default constructor and copy constructor).

如果不声明构造函数,则会获得为您自动生成的默认构造函数(默认构造函数和复制构造函数)。

When you do declare a constructor, you no longer get the auto-generated constructors (so you have to manually define the others if you want them).

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

Because you defined Vector2(A xp, A yp), the compiler no longer auto-generates Vector2() for you, and you have to define Vector2() yourself if you want to use it.

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

#5


1  

Take this part of your code:

拿这部分代码:

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

To the uninitiated C++ programmer it looks like this initialises Point1 and Point2, but it does not. Initialisation is either done by default (which cannot happen here, since Vector2<A> has no default constructor), or in the member initialisation list (which you didn't use); your assignments in the constructor body are just that: after-the-fact assignments.

对于不熟悉的C ++程序员来说,它看起来像初始化Point1和Point2,但事实并非如此。初始化要么默认完成(这里不会发生,因为Vector2 没有默认构造函数),或者在成员初始化列表中(你没有使用);你在构造函数体中的赋值就是:事后赋值。

You probably have your reasons for not giving Vector<A> a default constructor, so fix rayToCast by having its constructor properly initialise those Point1/Point2 members from the constructor arguments:

您可能有理由不给Vector 一个默认构造函数,因此通过让构造函数从构造函数参数中正确初始化那些Point1 / Point2成员来修复rayToCast:

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)
   : RAngle(angle)
   , point1(point1)
   , point2(point2)
{}

I'd also recommend tidying up your variable and argument names, as they confused me a little (caps here, not there, nowhere, everywhere, where, there!).

我还建议整理你的变量和参数名称,因为他们把我弄糊涂了一下(在这里上限,不在那里,无处,在任何地方,在哪里,那里!)。

#6


0  

As other's have already pointed out, the error is that you lack a default constructor. Use the syntax that Bo Persson suggested.

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

Another error is that variable names are case-sensitive, e.g., in the constructor of rayToCast you write:

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

this->Point1 = point1;

However, you named the class attribute point1. Note that it is not the same as:

但是,您将类属性命名为point1。请注意,它不同于:

this->point1 = point1;