【问题标题】:"error: no matching function for call to"“错误:没有匹配的调用函数”
【发布时间】:2012-12-28 20:58:54
【问题描述】:

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

这是输出:

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.

感谢任何帮助。

【问题讨论】:

标签: c++ templates constructor


【解决方案1】:

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

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

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

【讨论】:

  • 啊,每天我都知道我对 C++ 几乎一无所知。谢谢。
【解决方案2】:

您的代码中有两个问题:

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

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

需要给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;
    }
};

另外,你有错字,应该是 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
};

【讨论】:

  • 问题是缺少默认构造函数。大小写完全不相关,也不一定是问题。
  • 主要问题是默认构造函数,但是当我看到它时,为什么不指出呢?
  • 所以, :p 错过了它们在构造函数中被错误使用的地方。道歉。
【解决方案3】:

错误与模板无关。

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

rayToCast 的构造函数中使用成员初始化列表或在Vector2 中创建默认构造函数。

【讨论】:

    【解决方案4】:

    当你不声明构造函数时,你会得到为你自动生成的默认构造函数(默认构造函数和复制构造函数)。

    当你声明一个构造函数时,你不再得到自动生成的构造函数(所以你必须手动定义其他的如果你想要它们)。

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

    【讨论】:

      【解决方案5】:

      获取这部分代码:

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

      对于没有经验的 C++ 程序员来说,它看起来像这样初始化了 Point1Point2,但实际上并没有。初始化要么默认完成(这里不可能发生,因为Vector2&lt;A&gt; 没有默认构造函数),要么在成员初始化列表(你没有使用)中完成;您在构造函数 body 中的分配就是:事后分配。

      你可能有理由不给 Vector&lt;A&gt; 一个默认构造函数,所以修复 rayToCast 的构造函数正确初始化构造函数参数中的那些 Point1/Point2 成员:

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

      我还建议整理你的变量和参数名称,因为它们让我有点困惑(大写here,not there,nowhere,e​​verywhere,where,there!)

      【讨论】:

        【解决方案6】:

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

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

        this->Point1 = point1;
        

        但是,您将类属性命名为 point1。请注意,它与以下内容不同:

        this->point1 = point1;
        

        【讨论】:

          猜你喜欢
          • 2013-05-05
          • 2015-03-31
          • 1970-01-01
          • 2016-01-18
          • 1970-01-01
          • 1970-01-01
          • 2022-06-14
          相关资源
          最近更新 更多