Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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
    0_1552046527206_Qt Classes Error.png


  • Moderators

    @jefazo92 said in Problem Creating Classes in Qt Creator:

    #include "cylinder.cpp"

    Don't include the source file. That is never needed.


  • Lifetime Qt Champion

    @jefazo92 To add to @sierdzio : take a look at the constructor of your class and how your declare the instance (the compiler actually tells you what is wrong).
    By the way this problem isn't Qt specific, it is plain C++ problem in your code.


  • Lifetime Qt Champion

    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.
    alt text
    which is compiler way off saying Hey, you call constructor with no parameters
    but i cannot find one that is like that.


  • Lifetime Qt Champion

    Hi,

    In addition to my developer fellows, one alternative is to give r and h default values in your constructor declaration.


Log in to reply