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

inheritance problem



  • Hello,

    I want to create a super class Capteur_ that is a QObject class, and I will create sub classes but I had errors , I tried also to make a multiple inheritance for the sub class (Caplteur_ and QObject) but didn't work

    so this is my Capteur_.h

    #ifndef CAPTEUR__H
    #define CAPTEUR__H
    
    #include <QObject>
    #include<QTimer>
    
    class Capteur_ : public QObject
    {
        Q_OBJECT
    public:
        explicit Capteur_(int pin,QObject *parent = nullptr);
    protected:
        int m_pin;
        int m_value;
        int m_typeIO;
        int readPin();
        QTimer *m_timer;
    signals:
    protected slots:
        void onTimeout();
    public slots:
    };
    
    #endif // CAPTEUR__H
    

    this is my Capteur_.cpp

    #include "capteur_.h"
    
    Capteur_::Capteur_(int pin,QObject *parent) : QObject(parent)
    {
        m_pin=pin;
       }
    

    this is my Capteur_Output.h

    #ifndef CAPTEUR_OUTPUT_H
    #define CAPTEUR_OUTPUT_H
    #include"capteur_.h"
    
    class Capteur_Output:  public QObject , public Capteur_
    {  Q_OBJECT
    public:
         Capteur_Output(int pin,QObject *parent = nullptr);
     };
    
    #endif // CAPTEUR_OUTPUT_H
    

    and this is my Capteur_Output.cpp

    #include "capteur_output.h"
    
    
    Capteur_Output::Capteur_Output(int pin,QObject *parent):QObject(parent)
    {
        m_pin=pin;
    }
    
    I tried also to make my `Capteur_Output` class inherits only from `Capteur_` since `Capteur_` is a `QObject` class but didnt work also .. can someone please help me fixing this please


  • @amina said in inheritance problem:

    and if I add the other argument
    class Capteur_Output: public Capteur_
    {
    public:
    Capteur_Output(int pin,QObject *parent = nullptr);//,QObject *parent = nullptr

    };
    ***********Capteur_Output.cpp

    Capteur_Output::Capteur_Output(int pin,QObject *parent):Capteur_(parent)//,QObject *parent
    {
    m_pin=pin;
    }

    I have 2 remarks:

    • first, you should also add Q_OBJECT in Capteur_Output, because it is sub-classing a QObject based class
    • second, as far as I can see, Capteur_ constructor as 2 parameters: int and QObject*

    ==> Capteur_Output::Capteur_Output(int pin,QObject *parent):Capteur_(pin,parent) {} should do the job ;)


  • Moderators

    QObject classes cannot be mixed in multiple inheritance.

    But your other attempt should work:

    I tried also to make my Capteur_Output class inherits only from Capteur_ since Capteur_ is a QObject class but didnt work also ..

    Can you show the code for this approach? What were error messages, how did it fail to work?



  • @sierdzio thank you for explaining, but it didn't also work I don't know if I am doing something wrong

    I just changed the Capteur_Output.h

    class Capteur_Output:  public Capteur_
    {
    public:
         Capteur_Output(int pin);//,QObject *parent = nullptr
    
    };
    

    and the .cpp

    
    Capteur_Output::Capteur_Output(int pin)//,QObject *parent
    {
        m_pin=pin;
    }
    

    and this is the error that I got
    error.png

    and if I add the other argument

    class Capteur_Output:  public Capteur_
    {
    public:
         Capteur_Output(int pin,QObject *parent = nullptr);//,QObject *parent = nullptr
    
    };
    ***********Capteur_Output.cpp
    
    Capteur_Output::Capteur_Output(int pin,QObject *parent):Capteur_(parent)//,QObject *parent
    {
        m_pin=pin;
    }
    
    

    it gives this error
    error2.png



  • @amina said in inheritance problem:

    and if I add the other argument
    class Capteur_Output: public Capteur_
    {
    public:
    Capteur_Output(int pin,QObject *parent = nullptr);//,QObject *parent = nullptr

    };
    ***********Capteur_Output.cpp

    Capteur_Output::Capteur_Output(int pin,QObject *parent):Capteur_(parent)//,QObject *parent
    {
    m_pin=pin;
    }

    I have 2 remarks:

    • first, you should also add Q_OBJECT in Capteur_Output, because it is sub-classing a QObject based class
    • second, as far as I can see, Capteur_ constructor as 2 parameters: int and QObject*

    ==> Capteur_Output::Capteur_Output(int pin,QObject *parent):Capteur_(pin,parent) {} should do the job ;)


  • Moderators

    @amina to add to @KroMignon ,

    From the compiler output, you're trying to call the default constructor of your class somewhere, and it doesn't exist, because pin has no default value



  • @KroMignon thank you it is working now



  • @J-Hilk said in inheritance problem:

    default value

    thank you for explaining



  • @amina said in inheritance problem:

    it is working now

    great, so don't forget to mark your post as solved!