inheritance problem
-
Hello,
I want to create a super class
Capteur_
that is aQObject
class, and I will create sub classes but I had errors , I tried also to make a multiple inheritance for the sub class (Caplteur_
andQObject
) but didn't workso 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.cppCapteur_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
inCapteur_Output
, because it is sub-classing a QObject based class - second, as far as I can see,
Capteur_
constructor as 2 parameters:int
andQObject*
==>
Capteur_Output::Capteur_Output(int pin,QObject *parent):Capteur_(pin,parent) {}
should do the job ;) - first, you should also add
-
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 fromCapteur_
sinceCapteur_
is aQObject
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
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; }
-
@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.cppCapteur_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
inCapteur_Output
, because it is sub-classing a QObject based class - second, as far as I can see,
Capteur_
constructor as 2 parameters:int
andQObject*
==>
Capteur_Output::Capteur_Output(int pin,QObject *parent):Capteur_(pin,parent) {}
should do the job ;) - first, you should also add
-
@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
-
-
@amina said in inheritance problem:
it is working now
great, so don't forget to mark your post as solved!
1/8