Class with more than one constructor causes trouble - why?



  • 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?


  • Moderators

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



  • 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 :/



  • 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



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


  • Qt Champions 2016

    • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.