【问题标题】:'No matching function call' for subclass/superclass子类/超类的“没有匹配的函数调用”
【发布时间】:2017-09-06 05:07:54
【问题描述】:

我收到一个“没有匹配的函数调用”错误,我无法弄清楚如何摆脱它,这似乎与我的子类未被识别为超类有关。我有一个带有子类 Cube 的超类 Geometry,声明为:

class Cube : public Geometry {
    //code
    Intersection intersect(const Ray& ray_in, bool& intersected) const;
};

而 Cube 有一个返回 Intersection 的方法:

Intersection Cube::intersect(const Ray& ray_in, bool& intersected) const {
    // code
    return Intersection(point, normal, t_near, this);   //point and normal are vec4, t_near is double
}

我有一个 Intersection 构造函数:

Intersection(const glm::vec4& _point, const glm::vec4& _normal, Geometry* _geometry, const double _t);

但是当我尝试编译时,我的 Cube::intersect 方法中的返回行给出了错误:

no matching function for call to 'Intersection::Intersection(glm::vec4&, glm::vec4&, float&, const Cube*)'
   return Intersection(point, normal, t_near, this);
                                                  ^

为什么它不能识别 Cube 是 Geometry 的子类并尝试调用正确的 Intersection 构造函数?

【问题讨论】:

    标签: c++ subclass superclass


    【解决方案1】:

    这似乎与子类没有任何关系。构造函数的参数简直是错误的:

    return Intersection(point, normal, t_near, this); 
    

    不清楚t_near 是什么,但很可能是double。您将 double 作为第三个参数传递给构造函数,并将 this 作为第四个参数传递给构造函数,这显然必须是某种指针。在继续之前,请记住这一点......

    然后,您还声称您的构造函数声明如下:

    Intersection(const glm::vec4& _point, const glm::vec4& _normal,
                 Geometry* _geometry, const double _t)
    

    这就是您的问题中显示的内容。现在,问问自己:这个构造函数的第三个参数是double,第四个参数是某种指针,因为你在return 语句中构造这个类的一个实例?

    【讨论】:

    • 是的,我没有检查我传递参数的顺序,然后才假设它与类继承有关......谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-05-08
    • 2019-09-25
    • 2015-09-21
    • 2016-01-31
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多