[英]“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;
}
您的代碼中有兩個錯誤:
首先是您使用不存在的默認構造函數,而不是使用兩個參數定義的默認構造函數。
第二個是在不帶任何參數的情況下將兩個參數傳遞給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個參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.