Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem Creating Classes in Qt Creator

Problem Creating Classes in Qt Creator

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 650 Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jefazo92
    wrote on last edited by
    #1

    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

    sierdzioS jsulmJ 2 Replies Last reply
    0
    • J jefazo92

      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

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @jefazo92 said in Problem Creating Classes in Qt Creator:

      #include "cylinder.cpp"

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

      (Z(:^

      1 Reply Last reply
      5
      • J jefazo92

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        4
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          2
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved