Problem Creating Classes in Qt Creator
-
Hi everyone,
It is the first time I am writing classes with 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 ()". 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.
Header File (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)
#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
#include <iostream> #include "cylinder.h" #include "cylinder.cpp" using namespace std; int main() { Cylinder c; cout<< c.volume(5,5); return 0; }
Error Print Screen
-
@jefazo92 said in Problem Creating Classes in Qt Creator:
#include "cylinder.cpp"
Don't include the source file. That is never needed.
-
-
Hi
Your constructor is
Cylinder::Cylinder(double r,double h) << wants 2 doubles.
and your Cylinder::volume() takes no parameters.
So i think what you mean is.int main()
{
Cylinder c(5,5);
cout<< c.volume();return 0;
}
And as @jsulm points out, the compiler actually try to tell you that.
which is compiler way off saying Hey, you call constructor with no parameters
but i cannot find one that is like that. -
Hi,
In addition to my developer fellows, one alternative is to give
r
andh
default values in your constructor declaration.