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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:58:13+00:00 2026-05-20T04:58:13+00:00

I am creating a KeyValuePair class, and am running into some trouble when overloading

  • 0

I am creating a KeyValuePair class, and am running into some trouble when overloading the relational operators. It is my understanding that this is necessary for using the std sort functions (I am trying to have sort based on the values)

Here is the header:

template <typename K, typename V>
class KeyValuePair
{
public:
    //factory
    static KeyValuePair<K,V>* newKeyValuePair(K key, V value);  
    //getters
    const K &Key() const;
    const V &Value() const;
    //setter
    V &Value();

    //The problem
    bool operator<(const KeyValuePair<K,V> &rhs);

    string toString();
    ~KeyValuePair(void);
private:
    K key;
    V value;
    KeyValuePair(K key, V value);
    KeyValuePair(void);
};

Here is the definition of the < function

template <typename K, typename V>
bool KeyValuePair<K,V>::operator<(const KeyValuePair<K,V> &rhs)
{
    return value < rhs.Value();
}

And here is the main where I am just testing the functionality of the class.

int _tmain(int argc, _TCHAR* argv[])
{
    KeyValuePair<char,int>* kvp1 = KeyValuePair<char, int>::newKeyValuePair('A',1);
    KeyValuePair<char,int>* kvp2 = KeyValuePair<char,int>::newKeyValuePair('B',10);
    cout << (kvp1 < kvp2) << "\n";
    return 0;
}

I have a breakpoint at the < function in my KeyValuePair class, and it is never activated.

Any Ideas? Thanks in advance.

  • 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-05-20T04:58:14+00:00Added an answer on May 20, 2026 at 4:58 am

    kvp1 and kvp2 are pointers to KeyValuePair<char, int> objects. They are not themselves KeyValuePair<char, int> objects.

    *kvp1 < *kvp2 would invoke your overloaded operator<. You cannot overload operator< for two pointer types because the built-in operator< for pointers will be used.

    std::pair can be used as a key-value pair. In any case, you almost certainly shouldn’t be dynamically creating objects of this type: you should prefer to avoid dynamic allocation wherever possible, especially explicit dynamic allocation. Instead, just use KeyValuePair<char, int> local variables:

    KeyValuePair<char, int> kvp1('A', 1);
    KeyValuePair<char, int> kvp2('B', 10);
    std::cout << (kvp1 < kvp2) << "\n"; // calls your operator< overload
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating MVC3 Razor Helper like Helper.BeginForm() says that it can be done using extension
Creating a class which holds some threads, performing tasks and finally calling a callback-method
Creating a class at runtime is done as follows: klass = Class.new superclass, &block
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
(creating a separate question after comments on this: Javascript redeclared global variable overrides old
Creating a simple RPG game, first time using XNA. Trying to get my character
Error creating bean with name 'sessionFactory' defined in class path resource [ApplicationContext.xml]: Invocation of
I creating a web application using JSF,Hibernate,Spring. I have added a filter for checking
Creating a calculator-like dialog, I noticed that quickly clicking on a button in IE
Creating an installer for possible remote systems so that if they do not 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.