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. How to specialize operator< on QPair or QSet for a certain class?
Forum Updated to NodeBB v4.3 + New Features

How to specialize operator< on QPair or QSet for a certain class?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 987 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.
  • mbruelM Offline
    mbruelM Offline
    mbruel
    wrote on last edited by mbruel
    #1

    Hello,
    I'm using a QPair<Element*, QSet<Element*>> that I put in a QSet and use as key of a QMap.

    I've a compilation issue:

    /opt/Qt5.5.1/5.5/gcc_64/include/QtCore/qpair.h:109: error: no match for 'operator<' (operand types are 'const QSet<Element*>' and 'const QSet<Element*>')
         Q_DECL_NOEXCEPT_EXPR(noexcept(p1.first < p2.first || (!(p2.first < p1.first) && p1.second < p2.second)))
    

    Hum ok, so I'm trying to specialize the operator< in the Header of my Element class like this:

    template <> bool operator< (const QPair<Element*, QSet<Element*>> &p1,
                               const QPair<Element*, QSet<Element*>> &p2)
    {
        return p1.first < p2.first || (!(p2.first < p1.first) && p1.second.size() < p2.second.size());
    }
    

    But it is still picking the generic one in qpair.h
    How can I do?

    PS: I've seen there are several syntaxes to specialize a template function, I don't manage to compile with the one without argument deduction. (number 3 here: https://stackoverflow.com/questions/8323530/c-templates-specialization-syntax) The < of the operator< seems to be the problem.

    PS2: if I remove the template<> like I was overloading the function I get a mulitple definition issue

     error: multiple definition of `operator<(QPair<Element*, QSet<Element*> > const&, QPair<Element*, QSet<Element*> > const&)'
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2
      • You don't need a template at all, just a global function declared in a header you include
      • p1.first < p2.first this compares the position in memory of those elements, I don't think it's what you want
      bool operator<(const QPair<Element*, QSet<Element*> > &p1, const QPair<Element*, QSet<Element*> > &p2)
      {
          return (*p1.first < *p2.first ? true : p1.second.size() < p2.second.size());
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      mbruelM 1 Reply Last reply
      4
      • VRoninV VRonin
        • You don't need a template at all, just a global function declared in a header you include
        • p1.first < p2.first this compares the position in memory of those elements, I don't think it's what you want
        bool operator<(const QPair<Element*, QSet<Element*> > &p1, const QPair<Element*, QSet<Element*> > &p2)
        {
            return (*p1.first < *p2.first ? true : p1.second.size() < p2.second.size());
        }
        
        mbruelM Offline
        mbruelM Offline
        mbruel
        wrote on last edited by mbruel
        #3

        @VRonin
        as I said, when I do that I get a multiple definition error.

        error: multiple definition of `operator<(QPair<Element*, QSet<Element*> > const&, QPair<Element*, QSet<Element*> > const&)'
        

        I guess this is due to the generated one by the QPair template...

        PS: I don't mind the order, and thus comparing the memory address for my first member, I just care about uniqueness

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          When it is in the header it needs to be inline to avoid the problem with multiple definitions. Or move it to the source file and only leave the declaration in the header.

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

          mbruelM 1 Reply Last reply
          5
          • Christian EhrlicherC Christian Ehrlicher

            When it is in the header it needs to be inline to avoid the problem with multiple definitions. Or move it to the source file and only leave the declaration in the header.

            mbruelM Offline
            mbruelM Offline
            mbruel
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            Thanks a lot, indeed it was just the inline keyword that was missing!

            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