简体   繁体   English

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

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

It is the first time I am writing classes with Qt. 这是我第一次用Qt编写类。 I have stored the declarations in a header file and the source code in in another cpp file but I am getting the error in main of no matching function to call to "Name of class: Name of class ()". 我已经将声明存储在头文件中,并将源代码存储在另一个cpp文件中,但是在没有匹配函数的主体中出现错误,无法调用“类名:类名()”。 I will write down my code and give a print screen of the error below this message. 我将写下代码,并在此消息下方给出错误的打印屏幕。 Please I am really struggling with this and the solution would help me a lot. 拜托,我真的为此感到挣扎,解决方案将对我有很大帮助。 Thanks in advance. 提前致谢。

Class header file (cylinder.h) 类头文件(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

Class source code (cylinder.cpp) 类源代码(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 file main.cpp文件

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

using namespace std;

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

    return 0;
}

Error message from Qt 来自Qt的错误消息

There are two errors in your code: 您的代码中有两个错误:

  1. First is that you use the non-existent default constructor, instead of the one you have defined taking two arguments. 首先是您使用不存在的默认构造函数,而不是使用两个参数定义的默认构造函数。

  2. The second is that you pass two arguments to the volume function, when it doesn't take any arguments. 第二个是在不带任何参数的情况下将两个参数传递给volume函数。

It seems you have misunderstood how to use objects and member functions. 您似乎误解了如何使用对象和成员函数。

You code should look something like 您的代码应该看起来像

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

You are missing default constructor 您缺少默认的构造函数

Cylinder() {
...
}

Or construct in main with 或主要用

Cylinder c(5.,5.);

Also

double Cylinder::volume()

does not accept 2 arguments. 不接受2个参数。

From the picture you posted you can see two errors: first error 从您发布的图片中,您可以看到两个错误: 第一个错误

-> "candidate expects 2 arguments, 0 provided" in line 9.You should correct with: ->第9行中的“候选人期望2个参数,提供0个”,您应使用以下命令进行更正:

Cylinder c(5.0, 5.0);

Second error 第二次错误

In line 10 of main.cpp you are calling method volume passing 2 arguments, but function volume does expect 0 argument. 在main.cpp的第10行中,您正在调用传递2个参数的方法卷,但是函数volume确实需要0个参数。 You should change that line with: 您应该使用以下方式更改该行:

cout<< c.volume();

One last thing, why are you including the .cpp file? 最后一件事,为什么要包含.cpp文件? You don't need it 你不需要

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

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