Help with hash_map

This is a discussion on Help with hash_map within the C++ Programming forums, part of the General Programming Boards category; Hi, I need some help with using the hash_map. Here is my code of declaration of my hash_map: Code: hash_map<const ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    37

    Question Help with hash_map

    Hi,

    I need some help with using the hash_map.

    Here is my code of declaration of my hash_map:
    Code:
    hash_map<const string, Contact*> namehash;
    Contact is a class defined by myself

    Here is the usage of the hash_map:
    Code:
    namehash[v[c].Name()] = &v[c];
    v is a vector of Contact, Name() returns a std::string
    So what's wrong with this? Just can't compile it. I included the necessary headers already, and I'm using the SGI STL.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,215
    The compiler error may help.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    37
    Code:
    536 c:\dev-c_~1\include\g__~1\stl_hashtable.h
     instantiated from `hashtable<pair<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >,Contact *>,const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >,hash<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > >,_Select1st<pair<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >,Contact *> >,equal_to<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > >,allocator<Contact *> >::_M_bkt_num(const pair<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >,Contact *> &, unsigned int) const'
    Code:
    930 c:\dev-c_~1\include\g__~1\stl_hashtable.h
     instantiated from `hashtable<pair<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >,Contact *>,const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >,hash<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > >,_Select1st<pair<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >,Contact *> >,equal_to<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > >,allocator<Contact *> >::resize(unsigned int)'
    Code:
    744 c:\dev-c_~1\include\g__~1\stl_hashtable.h
     instantiated from `hashtable<pair<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >,Contact *>,const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >,hash<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > >,_Select1st<pair<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >,Contact *> >,equal_to<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > >,allocator<Contact *> >::find_or_insert(const pair<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >,Contact *> &)'
    Code:
    181 c:\dev-c_~1\include\g__~1\stl_hash_map.h
     instantiated from here
    Code:
    531 c:\dev-c_~1\include\g__~1\stl_hashtable.h
     no match for call to `(const hash<const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > >) (const basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > &)'
    Does this help? So lengthy... So cryptic...

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,215
    Hmm okay maybe not .

    I have no idea, then.

    [edit]
    What is the exact type .Name() returns?

    Something to do with this?
    http://forums.devshed.com/c-programm...ing-55093.html
    [/edit]

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    37
    .Name() returns a std::string. So I guess the "fix" in the link you gave me is of no use because of this...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,629
    Checking SGI's documentation, one of the requirements for the hash_map is that the key type is assignable. This is not the case for a const string. Perhaps you should declare it as:
    Code:
    hash_map<string, Contact*> namehash;
    By the way, you may want to consider using a std::tr1::unordered_map instead of SGI's extension.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    37
    i actually tried this way first, but isn't working...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,629
    Quote Originally Posted by jasperleeabc
    i actually tried this way first, but isn't working...
    Well, I have never used SGI's hash_map, but the same syntax works with g++ 4.2.4's TR1 implementation:
    Code:
    #include <string>
    #include <vector>
    #include <tr1/unordered_map>
    
    class Contact
    {
    public:
        std::string Name() const
        {
            return Name_;
        }
    private:
        std::string Name_;
    };
    
    using namespace std;
    
    int main()
    {
        vector<Contact> v(1);
        vector<Contact>::size_type c = 0;
        tr1::unordered_map<string, Contact*> namehash;
        namehash[v[c].Name()] = &v[c];
    }
    You might as well switch to TR1's unordered_map since it will be included in more or less the same form in C++0x's standard library.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    37
    thanks, but I actually simply changed the string to const char* :P. Anyway, I need some other help now. Somehow, my use of the hash doesn't work...

    Declaration:

    Code:
    hash_map<const char*, Contact*> namehash;
    Usage:

    Code:
    char* t = (char*) ListBox_GetItemData(handle, s);
    
    *namehash[t].write(dialog);
    Compilation error:

    Code:
    427 jphonebookmain.cpp
     request for member `write' in `namehash.hash_map<const char *,Contact *,hash<const char *>,equal_to<const char *>,allocator<Contact *> >::operator []((&t))', which is of non-aggregate type `Contact *'
    Anyone have any idea what's going on?

    Thanks in advance.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    37
    Got it done. Problem solved already... I forgot to put the brackets around *namehash[t] ......

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,629
    Quote Originally Posted by jasperleeabc
    but I actually simply changed the string to const char* :P
    Unfortunately, that may only be a partial solution since you would not be taking full advantage of what C++ has to offer.

    Quote Originally Posted by jasperleeabc
    Problem solved already... I forgot to put the brackets around *namehash[t]
    Typically, we would write: namehash[t]->write(dialog);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    37
    oh yes, ->....... what was I thinking.......

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21