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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:53:39+00:00 2026-05-14T00:53:39+00:00

Using the STL C++ hash_map… class MyKeyObject { std::string str1; std::string str2; bool operator==(…)

  • 0

Using the STL C++ hash_map…

class MyKeyObject
{
    std::string str1;
    std::string str2;

    bool operator==(...) { this.str1 == that.str1 ... }
};

class MyData
{
    std::string data1;
    int data2;
    std::string etcetc;
};

like this…

MyKeyObject a = MyKeyObject(...);
MyData b = MyData(...);

stdext::hash_map <MyKeyObject, MyData> _myDataHashMap;
_myDataHashMap[ a ] = b;

I get a whole load of errors. Here are the first three…

Error 1 error C2784: ‘bool
std::operator <(const
std::_Tree<_Traits> &,const
std::_Tree<_Traits> &)’ : could not
deduce template argument for ‘const
std::_Tree<_Traits> &’ from ‘const
MyKeyObject’ c:\program files\microsoft
visual studio
8\vc\include\functional 143

Error 2 error C2784: ‘bool
std::operator <(const
std::basic_string<_Elem,_Traits,_Alloc>
&,const _Elem *)’ : could not deduce
template argument for ‘const
std::basic_string<_Elem,_Traits,_Alloc>
&’ from ‘const
Tasking::MyKeyObject’ c:\program
files\microsoft visual studio
8\vc\include\functional 143

Error 3 error C2784: ‘bool
std::operator <(const _Elem *,const
std::basic_string<_Elem,_Traits,_Alloc>
&)’ : could not deduce template
argument for ‘const _Elem *’ from
‘const MyDataObject’ c:\program
files\microsoft visual studio
8\vc\include\functional 143

…

If I set the key to something simple like an int all is well.

What am I doing wrong?! Maybe I need to do something with templates?

Is there a better (quicker?) way of accessing data using a custom key object like this?

  • 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-14T00:53:40+00:00Added an answer on May 14, 2026 at 12:53 am

    Try the following, worked for me in VS 2005. This is a solution for both VS2005 built-in hash_map type in stdext namespace as well as the boost unordered_map (preferred). Delete whichever you don’t use.

    #include <boost/unordered_map.hpp>
    #include <hash_map>
    
    class HashKey
    {
    public:
        HashKey(const std::string& key)
        {
            _key=key;
        }
        HashKey(const char* key)
        {
            _key=key;
        }
    
        // for boost and stdext
        size_t hash() const
        {
            // your own hash function here
            size_t h = 0;
            std::string::const_iterator p, p_end;
            for(p = _key.begin(), p_end = _key.end(); p != p_end; ++p)
            {
                h = 31 * h + (*p);
            }
            return h;
        }
        // for boost
        bool operator==(const HashKey& other) const
        {
            return _key == other._key;
        }
    
        std::string _key;
    };
    
    // for boost
    namespace boost
    {
        template<>
        class hash<HashKey>
        {
        public :
            std::size_t operator()(const HashKey &mc) const
            {
                return mc.hash();
            }
        };
    }
    
    // for stdext
    namespace stdext
    {
        template<>
        class hash_compare<HashKey>
        {
        public :
            static const size_t bucket_size = 4;
            static const size_t min_buckets = 8;
    
            size_t operator()(const HashKey &mc) const
            {
                return mc.hash();
            }
    
            bool operator()(const HashKey &mc1, const HashKey &mc2) const
            {
                return (mc1._key < mc2._key);
            }
        };
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        {
            stdext::hash_map<HashKey, int> test;
            test["one"] = 1;
            test["two"] = 2;
        }
    
        {
            boost::unordered_map<HashKey, int> test(8); // optional default initial bucket count 8
            test["one"] = 1;
            test["two"] = 2;
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Turns out it was an issue with my page loading… May 15, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer The underlying problem here is that the type being passed… May 15, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer I like option 1. Also you don't need conditions and… May 15, 2026 at 4:09 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.