繁体   English   中英

Qt中出现“无法调用匹配功能”错误

[英]“no matching function to call” error in Qt

这是我第一次用Qt编写类。 我已经将声明存储在头文件中,并将源代码存储在另一个cpp文件中,但是在没有匹配函数的主体中出现错误,无法调用“类名:类名()”。 我将写下代码,并在此消息下方给出错误的打印屏幕。 拜托,我真的为此感到挣扎,解决方案将对我有很大帮助。 提前致谢。

类头文件(cylinder.h)

#ifndef CYLINDER_H
#define CYLINDER_H


class Cylinder
{
private:
    double height;
    double radius;
public:
    Cylinder(double,double);
    double volume();
    double surfaceArea();
};

#endif // CYLINDER_H

类源代码(cylinder.cpp)

#include "cylinder.h"
#include<math.h>
#define PI 3.142

Cylinder::Cylinder(double r,double h) {
    radius=r;
    height=h; }

double Cylinder::volume(){
    return PI*radius*radius*height; }

double Cylinder::surfaceArea(){
    return 2.0*PI*radius*height+PI*radius*radius; }

main.cpp文件

#include <iostream>
#include "cylinder.h"
#include "cylinder.cpp"

using namespace std;

int main()
{
    Cylinder c;
    cout<< c.volume(5,5);

    return 0;
}

来自Qt的错误消息

您的代码中有两个错误:

  1. 首先是您使用不存在的默认构造函数,而不是使用两个参数定义的默认构造函数。

  2. 第二个是在不带任何参数的情况下将两个参数传递给volume函数。

您似乎误解了如何使用对象和成员函数。

您的代码应该看起来像

Cylinder c(5, 5);     // Pass two arguments here
cout<< c.volume();    // Pass no arguments here

您缺少默认的构造函数

Cylinder() {
...
}

或主要用

Cylinder c(5.,5.);

double Cylinder::volume()

不接受2个参数。

从您发布的图片中,您可以看到两个错误: 第一个错误

->第9行中的“候选人期望2个参数,提供0个”,您应使用以下命令进行更正:

Cylinder c(5.0, 5.0);

第二次错误

在main.cpp的第10行中,您正在调用传递2个参数的方法卷,但是函数volume确实需要0个参数。 您应该使用以下方式更改该行:

cout<< c.volume();

最后一件事,为什么要包含.cpp文件? 你不需要

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM