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

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.
  • A Offline
    A Offline
    amina
    wrote on 23 Mar 2021, 12:37 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
    • A amina
      23 Mar 2021, 13:21

      @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

      K Offline
      K Offline
      KroMignon
      wrote on 23 Mar 2021, 13:46 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)

      A 1 Reply Last reply 23 Mar 2021, 13:58
      4
      • S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 23 Mar 2021, 12:45 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(:^

        A 1 Reply Last reply 23 Mar 2021, 13:21
        2
        • S sierdzio
          23 Mar 2021, 12:45

          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?

          A Offline
          A Offline
          amina
          wrote on 23 Mar 2021, 13:21 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

          K J 2 Replies Last reply 23 Mar 2021, 13:46
          0
          • A amina
            23 Mar 2021, 13:21

            @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

            K Offline
            K Offline
            KroMignon
            wrote on 23 Mar 2021, 13:46 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)

            A 1 Reply Last reply 23 Mar 2021, 13:58
            4
            • A amina
              23 Mar 2021, 13:21

              @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 Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 23 Mar 2021, 13:48 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.

              A 1 Reply Last reply 23 Mar 2021, 13:58
              2
              • K KroMignon
                23 Mar 2021, 13:46

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

                A Offline
                A Offline
                amina
                wrote on 23 Mar 2021, 13:58 last edited by
                #6

                @KroMignon thank you it is working now

                Pablo J. RoginaP 1 Reply Last reply 23 Mar 2021, 13:58
                0
                • J J.Hilk
                  23 Mar 2021, 13:48

                  @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

                  A Offline
                  A Offline
                  amina
                  wrote on 23 Mar 2021, 13:58 last edited by
                  #7

                  @J-Hilk said in inheritance problem:

                  default value

                  thank you for explaining

                  1 Reply Last reply
                  0
                  • A amina
                    23 Mar 2021, 13:58

                    @KroMignon thank you it is working now

                    Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on 23 Mar 2021, 13:58 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

                    2/8

                    23 Mar 2021, 12:45

                    6 unread
                    • Login

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