Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. creating custom class gives a compiler error
Forum Updated to NodeBB v4.3 + New Features

creating custom class gives a compiler error

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.2k Views 3 Watching
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by mzimmers
    #1

    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...

    K 1 Reply Last reply
    0
    • mzimmersM mzimmers

      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...

      K Offline
      K Offline
      koahnig
      wrote on last edited by koahnig
      #2

      @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.

      Vote the answer(s) that helped you to solve your issue(s)

      mzimmersM 1 Reply Last reply
      1
      • K koahnig

        @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.

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        @koahnig same error.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          mzimmersM 1 Reply Last reply
          2
          • SGaistS SGaist

            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 ?

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

            @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?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              mzimmersM 1 Reply Last reply
              0
              • SGaistS SGaist

                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.

                mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #7

                @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.

                K 1 Reply Last reply
                0
                • mzimmersM mzimmers

                  @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.

                  K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  @mzimmers

                  The major problem was for sure spotted by @SGaist with the initialization in the implementation of your constructor.
                  I was too focused that the initialization of the base class was missing. ;)

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by mzimmers
                    #9

                    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.

                    K 1 Reply Last reply
                    0
                    • mzimmersM mzimmers

                      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.

                      K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #10

                      @mzimmers

                      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.

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0

                      • Login

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