Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8038661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:18:22+00:00 2026-06-05T03:18:22+00:00

I have a class roughly defined like below. Among others, it has a <

  • 0

I have a class roughly defined like below. Among others, it has a < comparison operator.

class DictionarySearchItem {

public:

    DictionarySearchItem(); 
    double relevance() const;
    bool operator<(const DictionarySearchItem& item) { return relevance() > item.relevance(); }

};

typedef std::vector<DictionarySearchItem> DictionarySearchItemVector;

I then use the class this way:

DictionarySearchItemVector searchItems;

for (unsigned i = 0; i < entries.size(); i++) {
    // ...
    // ...
    DictionarySearchItem item;
    searchItems.push_back(item);
}

However when I try to sort the vector:

std::sort(searchItems.begin(), searchItems.end());

I’m getting the following compilation error with MinGW.

/usr/include/c++/4.2.1/bits/stl_algo.h:91: erreur : passing 'const hanzi::DictionarySearchItem' as 'this' argument of 'bool hanzi::DictionarySearchItem::operator<(const hanzi::DictionarySearchItem&)' discards qualifiers

I don’t quite understand what is incorrect with my code and the error message is not clear to me. The same code compiles fine with MSVC2008. Any idea what could be the issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:18:24+00:00Added an answer on June 5, 2026 at 3:18 am

    You need to make the less-than operator const:

    bool operator<(const DictionarySearchItem& item) const { ... }
                                                       ^
    

    The reason is probably that sort relies on the fact that elements being compared cannot change as a result of the comparison. This can be enforced by having both sides of the < comparison be const, which means the operator has to be const, as well as it’s argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that roughly looks like this: public class ViewModel { public
So I have a PHP stack that looks roughly like this Class Stack {
I have an object roughly like so: class Hand(object): def __init__(self, finger_names, finger_lengths, nail_sizes):
This is (roughly) what I have: class A { public bool IsInUpdate = false;
I have a class that looks roughly like this: template<std::size_t dim> class Foo {
I have a class with a static method that looks roughly like: class X
I have a c#-class that looks roughly like this: class ImageContainer { Image image;
I have a django model roughly as shown below: class Event(db.Model): creator = db.ReferenceProperty(User,
I have a tree like structure roughly like this: class Node < ActiveRecord::Base belongs_to
I have: class BASE{ public: virtual void func1() = 0; }; Then I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.