Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. error overloading compare operator for struct

error overloading compare operator for struct

Scheduled Pinned Locked Moved Solved C++ Gurus
13 Posts 4 Posters 2.4k 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 1 Feb 2023, 17:55 last edited by
    #1

    Hi all -

    I'm trying to create a comparison operator for a struct I've defined. I cut out most of the code for brevity; here's what produces the error:

    // EquipmentItem must be a struct;
    // if we make it a QObject-based class, we lose the assignment operator.
    struct EquipmentItem
    {
        Q_GADGET
    public:
        QString m_name;
    };
    
    bool operator==(const EquipmentItem &lhs, const EquipmentItem &rhs)
    {
        bool rc = false;
        if (lhs.m_name == rhs.m_name)
            rc = true;
        return rc;
    }
    

    I'm getting the following errors at build time:

    C:\Qt\6.4.2\mingw_64\include\QtCore\qmetaobject.h:176: error: 'EquipmentItem' does not name a type
    C:\Qt\6.4.2\mingw_64\include\QtCore\qmetaobject.h:179: error: no match for 'operator==' (operand types are 'const QMetaMethod' and 'const QMetaMethod')
    

    So...

    1. can someone tell me what I'm doing wrong?
    2. is this even the best approach, or should I be defining the operator within the struct? I've seen examples with both approaches, but I can't get either to work.

    Thanks...

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote on 1 Feb 2023, 18:08 last edited by Axel Spoerl 2 Jan 2023, 18:17
      #4

      Hi @mzimmers.
      you are not really overloading operator==. You have to define it within the struct (or implement it directly). For that purpose you have to have in mind that this will be an implicit argument within the struct, so you only have to mention it in an implementation out of line.
      That said, something like this should ferry you across the river:

      a) direct implementation

      // EquipmentItem must be a struct;
      // if we make it a QObject-based class, we lose the assignment operator.
      struct EquipmentItem
      {
          Q_GADGET
      public:
          QString m_name;
          bool operator==(const EquipmentItem &left) const
          {
              return left.m_name == m_name;
          }
      };
      

      b) definition and implementation out of line

      // EquipmentItem must be a struct;
      // if we make it a QObject-based class, we lose the assignment operator.
      struct EquipmentItem
      {
          Q_GADGET
      public:
          QString m_name;
          bool operator==(const EquipmentItem &left) const;
      };
      
      // somewhere else
      bool EquipmentItem::operator==(const EquipmentItem &left, const EquipmentItem &right) const
      {
          return left.m_name == right.m_name;
      }
      
      

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      2
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 1 Feb 2023, 18:03 last edited by
        #2

        Works fine for me:

        #include <QtCore>
        struct EquipmentItem
        {
            Q_GADGET
        public:
            QString m_name;
        };
        
        bool operator==(const EquipmentItem& lhs, const EquipmentItem& rhs)
        {
            bool rc = false;
            if (lhs.m_name == rhs.m_name)
                rc = true;
            return rc;
        }
        
        int main(int argc, char* argv[])
        {
            QCoreApplication app(argc, argv);
            EquipmentItem item;
            return 0;
        }
        
        #include "main.moc"
        

        Please provide a minimal, compilable example.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JoeCFD
          wrote on 1 Feb 2023, 18:05 last edited by JoeCFD 2 Jan 2023, 18:05
          #3

          @mzimmers said in error overloading compare operator for struct:

          bool operator==(const EquipmentItem &lhs, const EquipmentItem &rhs)
          {
          bool rc = false;
          if (lhs.m_name == rhs.m_name)
          rc = true;
          return rc;
          }

          ??????????

          bool operator==(const EquipmentItem &rhs)
          {
              bool rc = false;
              if (this->m_name == rhs.m_name)
                  rc = true;
              return rc;
          }
          
          1 Reply Last reply
          0
          • A Offline
            A Offline
            Axel Spoerl
            Moderators
            wrote on 1 Feb 2023, 18:08 last edited by Axel Spoerl 2 Jan 2023, 18:17
            #4

            Hi @mzimmers.
            you are not really overloading operator==. You have to define it within the struct (or implement it directly). For that purpose you have to have in mind that this will be an implicit argument within the struct, so you only have to mention it in an implementation out of line.
            That said, something like this should ferry you across the river:

            a) direct implementation

            // EquipmentItem must be a struct;
            // if we make it a QObject-based class, we lose the assignment operator.
            struct EquipmentItem
            {
                Q_GADGET
            public:
                QString m_name;
                bool operator==(const EquipmentItem &left) const
                {
                    return left.m_name == m_name;
                }
            };
            

            b) definition and implementation out of line

            // EquipmentItem must be a struct;
            // if we make it a QObject-based class, we lose the assignment operator.
            struct EquipmentItem
            {
                Q_GADGET
            public:
                QString m_name;
                bool operator==(const EquipmentItem &left) const;
            };
            
            // somewhere else
            bool EquipmentItem::operator==(const EquipmentItem &left, const EquipmentItem &right) const
            {
                return left.m_name == right.m_name;
            }
            
            

            Software Engineer
            The Qt Company, Oslo

            1 Reply Last reply
            2
            • A Offline
              A Offline
              Axel Spoerl
              Moderators
              wrote on 1 Feb 2023, 18:12 last edited by Axel Spoerl 2 Jan 2023, 18:16
              #5

              @Christian-Ehrlicher kindly and competently reminded me that operator== has to be const.
              Thanks! (previous post edited for correction)

              Software Engineer
              The Qt Company, Oslo

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mzimmers
                wrote on 1 Feb 2023, 18:21 last edited by
                #6

                Well, I can't provide a compilable example, but here are my files:

                equipmentitem.h:

                #ifndef EQUIPMENTITEM_H
                #define EQUIPMENTITEM_H
                
                #include <QObject>
                
                // EquipmentItem must be a struct;
                // if we make it a QObject-based class, we lose the assignment operator.
                struct EquipmentItem
                {
                    Q_GADGET
                public:
                    QString m_name;
                    bool operator==(const EquipmentItem &rhs);
                };
                
                #endif // EQUIPMENTITEM_H
                

                equipmentitem.cpp:

                bool EquipmentItem::operator==(const EquipmentItem &rhs)
                {
                    bool rc = false;
                    do {
                        if (m_name != rhs.m_name)
                            continue;
                        rc = true;
                    } while (false);
                    return rc;
                }
                

                Trying to build with this in a CMake project yields the errors above.

                I notice that the errors are exposed in my qmetaobject.h file, so I deleted my build directory and re-ran CMake, etc. with no change in results.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 1 Feb 2023, 18:26 last edited by
                  #7

                  As always - simplify your program until it's short enough so we can compile it here or the error goes away - the task of a programmer.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    Axel Spoerl
                    Moderators
                    wrote on 1 Feb 2023, 18:28 last edited by
                    #8

                    Change the definition to
                    bool operator==(const EquipmentItem &rhs) const;

                    ...and the implementation to my proposal from the previous post.

                    Software Engineer
                    The Qt Company, Oslo

                    M 1 Reply Last reply 1 Feb 2023, 18:35
                    0
                    • A Axel Spoerl
                      1 Feb 2023, 18:28

                      Change the definition to
                      bool operator==(const EquipmentItem &rhs) const;

                      ...and the implementation to my proposal from the previous post.

                      M Offline
                      M Offline
                      mzimmers
                      wrote on 1 Feb 2023, 18:35 last edited by mzimmers 2 Jan 2023, 18:47
                      #9

                      @Axel-Spoerl I'm confused - which implementation should I be using? When I try this:

                      bool EquipmentItem::operator==(const EquipmentItem &lhs, const EquipmentItem &rhs) const {
                          ...
                      

                      I get an additional error:

                      C:\Users\Michael.Zimmers\Qt_projects\nga_demo\equipmentitem.cpp:28: error: Overloaded 'operator==' must be a binary operator (has 3 parameters)
                      

                      EDIT:

                      I can get rid of that error by coding this instead:

                      bool EquipmentItem::operator==(const EquipmentItem &rhs) const {
                      

                      But the original errors remain.

                      EDIT 2:

                      I took Christian's suggestion, and put those files in an otherwise empty project, and it builds without error. So, I guess this is no longer a C++ issue.

                      Thanks for all the assistance.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Axel Spoerl
                        Moderators
                        wrote on 1 Feb 2023, 18:47 last edited by
                        #10

                        The { at the end was my typo.
                        Best to use one of the options I have proposed above.

                        Software Engineer
                        The Qt Company, Oslo

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mzimmers
                          wrote on 1 Feb 2023, 20:30 last edited by
                          #11

                          OK, so I found the problem, and the operator overload was a red herring (though I was indeed doing it wrong).

                          It turns out that in my experimentation with the overload operator, I accidently changed this line in the qmetaobject.h file:

                          // from   
                           friend bool operator ==(const QMetaMethod &lhs, const QMetaMethod &m2)
                          // to
                          friend EquipmentItem operator ==(const QMetaMethod &lhs, const QMetaMethod &m2)
                          

                          Oops. I think this happened when I mindlessly clicked on a light bulb icon...I should have been more careful.

                          To find this, I just needed to look at the complete error messages (which I didn't post here) a little more carefully.

                          Two lessons from this:

                          1. My Qt installation should probably be readonly, yes?
                          2. Might be better to use github rather than the installer for downnloading Qt, so that I can easily see when a file got changed.

                          Anyway, thanks again for looking.

                          M 1 Reply Last reply 12 Feb 2023, 16:39
                          3
                          • A Offline
                            A Offline
                            Axel Spoerl
                            Moderators
                            wrote on 1 Feb 2023, 20:50 last edited by
                            #12

                            Thanks for sharing!
                            Regarding 1) and 2) - git is my preference.
                            (Comes with a handy advantage: When you find a way to make Qt even better, you can just push it ;-)

                            Software Engineer
                            The Qt Company, Oslo

                            1 Reply Last reply
                            0
                            • M mzimmers
                              1 Feb 2023, 20:30

                              OK, so I found the problem, and the operator overload was a red herring (though I was indeed doing it wrong).

                              It turns out that in my experimentation with the overload operator, I accidently changed this line in the qmetaobject.h file:

                              // from   
                               friend bool operator ==(const QMetaMethod &lhs, const QMetaMethod &m2)
                              // to
                              friend EquipmentItem operator ==(const QMetaMethod &lhs, const QMetaMethod &m2)
                              

                              Oops. I think this happened when I mindlessly clicked on a light bulb icon...I should have been more careful.

                              To find this, I just needed to look at the complete error messages (which I didn't post here) a little more carefully.

                              Two lessons from this:

                              1. My Qt installation should probably be readonly, yes?
                              2. Might be better to use github rather than the installer for downnloading Qt, so that I can easily see when a file got changed.

                              Anyway, thanks again for looking.

                              M Offline
                              M Offline
                              mzimmers
                              wrote on 12 Feb 2023, 16:39 last edited by
                              #13

                              @mzimmers said in error overloading compare operator for struct:

                              My Qt installation should probably be readonly, yes?

                              Definitely NO. It might be OK to lock down some areas, but I just finished chasing down a problem due to making some android templates readonly -- it caused my builds to break. Just a cautionary note...

                              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