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. Class with more than one constructor causes trouble - why?
Forum Updated to NodeBB v4.3 + New Features

Class with more than one constructor causes trouble - why?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.2k Views 2 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.
  • H Offline
    H Offline
    haxxe
    wrote on last edited by
    #1

    hey guys, i wrote myself a led class, but i have several questions:
    my constructors are:

    public:
        LED(){};
    
        LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, int updateTimeIntervall);
        LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, int _grayscale, int updateTimeIntervall);
    
        LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, float _brightness, int updateTimeIntervall);
    
        LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, int _grayscale, int _updateTimeIntervall, float _fade_speed);
    
        LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, float _brightness, int _updateTimeIntervall, float _fade_speed);
    
        LED(const LED& orig);
    
        virtual ~LED();
    

    Now my problems: if i DONT have an completely empty constructor like

      LED(){};
    

    my mainWindows is complaining about:

    /home/daniel/Sample/mainwindow.cpp:28: error: no matching function for call to ‘LED::LED()’
         ui(new Ui::MainWindow)
                              ^
    

    And if i put an empty constructor with LED(){} it says:

    /home/daniel/Sample/LED.h:52: error: uninitialized const member in ‘const int’ [-fpermissive]
         LED(){};
         ^
    

    and he wents on complaining about every const int variable which are in the OTHER constructors.
    why is this?

    K 1 Reply Last reply
    0
    • H haxxe

      hey guys, i wrote myself a led class, but i have several questions:
      my constructors are:

      public:
          LED(){};
      
          LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, int updateTimeIntervall);
          LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, int _grayscale, int updateTimeIntervall);
      
          LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, float _brightness, int updateTimeIntervall);
      
          LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, int _grayscale, int _updateTimeIntervall, float _fade_speed);
      
          LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, float _brightness, int _updateTimeIntervall, float _fade_speed);
      
          LED(const LED& orig);
      
          virtual ~LED();
      

      Now my problems: if i DONT have an completely empty constructor like

        LED(){};
      

      my mainWindows is complaining about:

      /home/daniel/Sample/mainwindow.cpp:28: error: no matching function for call to ‘LED::LED()’
           ui(new Ui::MainWindow)
                                ^
      

      And if i put an empty constructor with LED(){} it says:

      /home/daniel/Sample/LED.h:52: error: uninitialized const member in ‘const int’ [-fpermissive]
           LED(){};
           ^
      

      and he wents on complaining about every const int variable which are in the OTHER constructors.
      why is this?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @haxxe

      How do you use LED in your GUI?
      Apparently you have something in your GUI requiring an empty constructor. Sometimes these are container which need an empty constructor. Most of the time it is not obvious. At least that is my experience with stl containers.

      Furthermore, you seem to assign a const int in your other constructors, which is not assigned in your empty constructor. Ensure this assignment in your empty constructor and your problem shall be solved.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2
      • H Offline
        H Offline
        haxxe
        wrote on last edited by
        #3

        hi, thanks for your answer.
        im controlling leds over canbus.
        and i implemented the function into the mainwindow.cpp. Its just included now, im not using any functions.
        after assigning some values to some consts in my LED.h, it works.
        but it complains that the other constructors are assigning values to readonly variables :/

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          no matching function for call to ‘LED::LED()’

          If you don't have a default constructor and you have a LED member variable in your mainwindow then you have to pass the relevant argument in the initialiser list of the mainwindow constructor. alternatively change it to a pointer member and construct it when you need it.

          uninitialized const member in ‘const int’

          const means that it can only be set at construction, this warns you that you have to set it in the constructor otherwise it's useless

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3
          • H Offline
            H Offline
            haxxe
            wrote on last edited by haxxe
            #5

            but i dont have a membervariable in my mainwindow, my programm still works, even if i havent led.h included.
            now, its just a class inside my project without any usages and im trying out why it fails to compile :/

            EDIT: oh alright, yes im using my led class inside my mainwindow.h.

            EDIT2: i dont really get it.
            if i dont assign it, it fails, if i assign it, it fails
            LED.h

            class LED
            {
            private:
                const int number;			
                const float angle_height;	
                const float angle_around;	
            
                const float radius;			
            
                const float x;				
                const float y;				
                const float z; 				
            
                int grayscale;
            public:
                LED(){};
            
                LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, int updateTimeIntervall);
                LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, int _grayscale, int updateTimeIntervall);
                LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, float _brightness, int updateTimeIntervall);
                LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, int _grayscale, int _updateTimeIntervall, float _fade_speed);
                LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, float _brightness, int _updateTimeIntervall, float _fade_speed);
                LED(const LED& orig);
                virtual ~LED();		
            

            LED.cpp

            LED::LED(const int _LED_number, const float _angle_height, const float _angle_around, const float _radius, int _grayscale, int _updateTimeIntervall)
            {
                number = _LED_number;
                angle_around = _angle_around;
                angle_height = _angle_height;
                radius = _radius;
            
                set_grayscale(_grayscale);
                set_updateTimeIntervall(_updateTimeIntervall);
            
                fade_state = FADE_NO;
            }
            

            here it says im assigning values to readonly variables. and in the signature it says everything is uninitialized.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6
              • EDIT2: i dont really get it.

              Hi as @VRonin said initialiser list is the key here.
              When you use const there are special rules and you cannot just
              have empty constructor.
              The const MUST be assigned a value and its an error if you dont supply that.

              class LED {
                  const x,y,...
              public:
                  LED()  : x(0), y(0) and so on {};
              

              http://www.cprogramming.com/tutorial/initialization-lists-c++.html

              1 Reply Last reply
              3

              • Login

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