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 7831605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:48:21+00:00 2026-06-02T11:48:21+00:00

I have a problem with overloading of the < operator. I have this class:

  • 0

I have a problem with overloading of the < operator.
I have this class:

WordEntry.h:

class WordEntry
{
public:
    WordEntry(string word);
    ~WordEntry();

    bool operator<(const WordEntry otherWordEntry);

    string getWord();

private:
    string _word;
};

WordEntry.cpp(I removed constructor & destructor):

string WordEntry::getWord()
{
   return _word;
}


bool WordEntry::operator<(WordEntry otherWordEntry)
{
   return  lexicographical_compare(_word.begin(),_word.end(),otherWordEntry.getWord().begin(),otherWordEntry.getWord().end());
}

Everything is fine when I’m using it in main.cpp like that:

    WordEntry w1("Der");
    WordEntry w2("das");

    if (w1.operator<(w2)) {
       cout << "w1 > w2";
    }
    else 
    {
       cout << "w2 > w1";
    }

But when I call sort() on a vector with WordEntry objects, I’ll get the error message

Invalid operands to binary expression (‘const WordEntry’ and ‘const
WordEntry’)

and it points to stl_algo.h.

Does anyone knows what’s going on here?

  • 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-02T11:48:22+00:00Added an answer on June 2, 2026 at 11:48 am

    Right now the argument to < is const but the member is not. This means a < comparison between 2 const WordEntry& objects will fail because it can’t bind to <. You need to make the member and the argument both const

    bool operator<(const WordEntry& otherWordEntry) const;
    
    bool WordEntry::operator<(const WordEntry& otherWordEntry) const {
      ...
    }
    

    Note: As pointed out in the comments you should also pass WordEntry by reference

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

Sidebar

Related Questions

I have this: typedef string domanin_name; And then, I try to overload the operator<
I have a class using operator overloading, but there are some warnings. // base.h
I have a container class that is templatized. I am overloading the assignment operator
I have a class Terminallog which is overloading the << operator. If I do
I have some problem about operator overloading. I looked everywhere but couldn't find a
For training, I'm trying to write a class like std::string. I have read that
I have some problems overloading operators with template members and using make_pair: class MyArchive
I'm overloading operator new , but I recently hit a problem with alignment. Basically,
Here's an overview of my problem: I have a templated class called TWMatrix. I've
Ok, So I've googled this problem and I have searched stack overflow but I

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.