[英]“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.