Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. inheritance problem
QtWS25 Last Chance

inheritance problem

Scheduled Pinned Locked Moved Solved Mobile and Embedded
inheritanceqobjectsubclassing
8 Posts 5 Posters 1.3k Views
  • 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.
  • aminaA Offline
    aminaA Offline
    amina
    wrote on last edited by amina
    #1

    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
    1 Reply Last reply
    0
    • aminaA amina

      @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

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #4

      @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 ;)

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      aminaA 1 Reply Last reply
      4
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #2

        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?

        (Z(:^

        aminaA 1 Reply Last reply
        2
        • sierdzioS sierdzio

          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?

          aminaA Offline
          aminaA Offline
          amina
          wrote on last edited by amina
          #3

          @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

          KroMignonK J.HilkJ 2 Replies Last reply
          0
          • aminaA amina

            @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

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #4

            @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 ;)

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            aminaA 1 Reply Last reply
            4
            • aminaA amina

              @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

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #5

              @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


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              aminaA 1 Reply Last reply
              2
              • KroMignonK KroMignon

                @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 ;)

                aminaA Offline
                aminaA Offline
                amina
                wrote on last edited by
                #6

                @KroMignon thank you it is working now

                Pablo J. RoginaP 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @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

                  aminaA Offline
                  aminaA Offline
                  amina
                  wrote on last edited by
                  #7

                  @J-Hilk said in inheritance problem:

                  default value

                  thank you for explaining

                  1 Reply Last reply
                  0
                  • aminaA amina

                    @KroMignon thank you it is working now

                    Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on last edited by
                    #8

                    @amina said in inheritance problem:

                    it is working now

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

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    1

                    • Login

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