簡體   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