Despite public Inheritance and public Base Class Constructor, the Derived Class has no access to its parent's constructor
-
I have a Base Class named "GroupBoxWidget" and two children named "GroupBoxIn" and "GroupBoxOut". Both of them try to construct themselves using its parent as an initialisation. At this point the IntelliSense says that the CGroupBox's constructor is inaccessible.
Let's see that in details, shall we? Beginning with the parent...
GroupBoxWidget.h#ifndef MY_GROUPBOX_H #define MY_GROUPBOX_H #include <QGroupBox> class CGroupBoxWidget: public QGroupBox { public: Q_OBJECT CGroupBoxWidget(QWidget* pParent = nullptr); virtual ~CGroupBoxWidget(); protected: //Create Widgets virtual QGroupBox *CreateBtnsGroupBox() = 0; private: }; #endif // !MY_GROUPBOX_H
As you can see, the QGroupBoxWidget itself is derived from QGroupBox from Qt's library. Nothing is declared as private. The pure virtual function "=0" will be overriden in the children. Let's take a fast look into it's constructor in GroupBoxWidget.cpp:
//Constructor CGroupBoxWidget::CGroupBoxWidget(QWidget* pParent) : QGroupBox(pParent) { //Every MainGroupBoxWidget integrates at least two Labels, a ComboBox and a PushButton }
Let's now choose a child to examinate. Looking at GroupBoxIn.h:
#ifndef GROUPBOXIN_H #define GROUPBOXIN_H #include "GroupBoxWidget.h" class CGroupBoxIn : public CGroupBoxWidget { public: Q_OBJECT CGroupBoxIn(QWidget* pParent = nullptr); virtual ~CGroupBoxIn(); protected: virtual QGroupBox *CreateBtnsGroupBox() override; QButtonGroup *m_pButtonGroupIn; private: }; #endif // !GROUPBOXIN_H
Everything is okay here. The pure virtual function overrides the one from the base's class. But when I try to construct it in the GroupBoxIn.cpp I got a fine red line below the Initialization List through the parent class exactly at "CGroupBoxWidgetidget(pParent)"
CGroupBoxIn::CGroupBoxIn(QWidget* pParent) : CGroupBoxWidget(pParent) { this->setTitle("Input"); }
I would like to hear your suggestions about from where this compiler error comes from. I'm usinf MSVC 15, Qt 5.9, C++11 and Windows 11.
-
@Arantxa
Do you mean your code compiles OK? Are you using Visual Studio? If so it's a VS intellisense issue. Are you using Qt Creator? In which case what Code Completer are you using. Frankly neither clang nor the simpler inbuilt one are perfect, there are plenty of situations where they get it "wrong".You are supposed to put
Q_OBJECT
macro as first thing before anypublic:
etc., like:class CGroupBoxIn : public CGroupBoxWidget { Q_OBJECT public:
Your way, if by any chance it expands to something which ends in
private:
or similar, the methods you think arepublic
will not be. I have not looked at what it does expand to.I'm usinf MSVC 15, Qt 5.9, C++11
BTW, this is pretty outdated. There are more recent versions of Qt5 & Qt6.
-
@JonB Thank you for your answer. The Q_OBJECT declared in public access was what I was struggling in.
You asked me: "Do you mean your code compiles OK? " It showed up when I clicked in "Build" and the error category was setted as "IntelliSense" in the Output Window.
I'm not using Qt Creator and this version is what my company has a licence for the moment.
-
@Arantxa
Good for theQ_OBJECT
positioning.I was not sure, the "error category Intellisense" might be only an IDE warning not a compiler warning. But this doesn't matter now as you have resolved.
-
@JonB said in Despite public Inheritance and public Base Class Constructor, the Derived Class has no access to its parent's constructor:
the "error category Intellisense" might be only an IDE warning
That.