Solved creating custom class gives a compiler error
-
Based on some advice I received in another topic, I'm creating a custom class. Here's a little of the code:
class CD_ComboBox : public QComboBox { Q_OBJECT public: CD_ComboBox(QComboBox *parent); void showPopup(); void hidePopup(); void findPorts(); }; ... CD_ComboBox::CD_ComboBox(QComboBox *parent = Q_NULLPTR) { }
I'm inexperienced with creating inheriting classes, so I'm probably doing something wrong, but I'm getting a strange error:
C:\Users\MZimmers\Qt projects\build-APGDupontProvisioner-Desktop_Qt_5_9_1_MinGW_32bit-Debug\ui_widget.h:256: error: no matching function for call to 'CD_ComboBox::CD_ComboBox(QGroupBox&) portComboBox = new CD_ComboBox(groupBoxUserPwd);
Why is it referencing the containing group box? Does this have something to do with my promotion of the object from QComboBox to CD_ComboBox?
Thanks...
-
@mzimmers
Please, try this:CD_ComboBox::CD_ComboBox(QComboBox *parent = Q_NULLPTR) : QComboBox ( parent ) { }
I am not sure about the rest of your error, but let's see.
-
@koahnig same error.
-
Hi,
QComboBox *parent = Q_NULLPTR
is only allowed when declaring the method (or if the declaration and the implementation is in the same place). Thus just move that assignment into the header.Out of curiosity, why are you imposing QComboBox as object parent ? Why not QWidget ?
-
@SGaist I don't have a good answer for that. Isn't the object parent a member of the base class, or am I confusing concepts here?
-
No, it doesn't so I'd say yes you are confusing this with something else.
By default, Qt's QWidget derived classes take a parent of type QWidget so you can have anything that is a QWidget as a parent. Otherwise you restrict yourself for no good reasons.
-
@SGaist OK, well it works with QWidget. And with the added line suggested by koahnig, my compiler warning went away (a side benefit).
Thank you both.
-
-
I'm still getting a Clang error from this, though:
C:\Users\MZimmers\Qt projects\APGDupontProvisioner\cd_combobox.cpp:9:35: error: addition of default argument on redeclaration makes this constructor a default constructor CD_ComboBox::CD_ComboBox(QWidget *parent = Q_NULLPTR) ^ ~~~~~~~~~ C:\Users\MZimmers\Qt projects\APGDupontProvisioner/cd_combobox.h:15:5: note: previous declaration is here CD_ComboBox(QWidget *parent); ^ 1 error generated.
I have no idea what this even means -- don't I want my constructor to override the one from the base object?
EDIT: I eliminated the error by moving the assignment from the definition to the declaration. Honestly I don't see why Clang would call this an error, though.
EDIT 2: OK, I'll buy this explanation for why it should be this way, though it should be a warning, not an error, IMO.
-
Those are the rules :)
You need to have it with the declaration in the header, because this is used everywhere. When no value is given in the call, the compiler will simply introduce the value given in the header.
When having it in the implementation file, it is only there. This could potentially available at runtime, but not really logic. To allow also an initialization in the implementation, which could be potentially different would confuse only.