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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:38:25+00:00 2026-06-14T15:38:25+00:00

Trying to brush up on my C++ and STL proficiency, running into a problem

  • 0

Trying to brush up on my C++ and STL proficiency, running into a problem with std::map keyed by a structure I’ve defined. Relevant code:

typedef struct key_t {
   int a;
   int b;
   bool operator==(const key_t& rhs)
   {
      return (a == rhs.a) && (b == rhs.b);
   }
   bool operator<(const key_t& rhs) //added the when I saw this error, didn't help
   {
      return a < rhs.a;
   }
} key_t;

std::map<key_t, int> fooMap;

void func(void)
{
    key_t key;        
    key.a = 1;
    key.b = 2;

    fooMap.insert(std::pair<key_t, int>(key, 100));
}

Error looks like this:

"/opt/[redacted]/include/functional", line 133: error: no operator "<" matches these operands
            operand types are: const key_t < const key_t
          detected during:
            instantiation of "bool std::less<_Ty>::operator()(const _Ty &, const _Ty &) const [with _Ty=key_t]" at line 547 of "/opt/[redacted]/include/xtree"
instantiation of "std::_Tree<_Traits>::_Pairib std::_Tree<_Traits>::insert(const std::_Tree<_Traits>::value_type &) [with _Traits=std::_Tmap_traits<key_t, UI32, std::less<key_t>, std::allocator<std::pair<const key_t, UI32>>, false>]"

What am I doing wrong? Is it just flat-out awful/impossible to use structures as a map key? Or something else I’m overlooking?

  • 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-14T15:38:27+00:00Added an answer on June 14, 2026 at 3:38 pm

    This

     bool operator<(const key_t& rhs)
    

    needs to be a const method

     bool operator<(const key_t& rhs) const
    

    The two are different signatures, and std::less looks for the latter. The latter, as a const method, implying that it won’t modify the object. The former however without const may imply that a modification to this may be performed.

    In general its a good idea to have const methods, even if you can cast in away, it implies a promise to the client that no modifications will take place.

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

Sidebar

Related Questions

While trying to brush up my Ruby skills I keep running across this case
Hello I am trying to create a brush but I am having problem with
I'm trying to convert an enumeration into a brush so that an enum variable
I'm trying to brush up my algorithmic skills and came across this problem. Here
I'm trying to brush up on my design pattern skills, and I'm curious what
I'm trying to implement OpenGL brush for mac os. Using GLPaint as sample. Main
I'm trying to implement a painting app with brush texture and blending, similar to
How can I convert a System.Windows.Media.Brush to System.Drawing.Brush? I'm trying to get the color
I'm trying to brush up on my big o calculations. If I have function
I'm trying to add a converter to a DataGridTextColumn to convert the foreground brush

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.