This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: gcc-3.0.2 + hash_map<string,int> compilation problems


Hi!

I have had the same issue and solved it by declaring:

#include <string>
#include <ext/hash_map>

namespace __gnu_cxx
{
        template<> struct hash< std::string >
        {
                size_t operator()( const std::string& x ) const
                {
                        return hash< const char* >()( x.c_str() );
                }
        };
}

I guess that the reason for not having this declared in the std::string 
is that the hash* are extensions to the standard. Maybe something like 
the above could be added into the codebase somewhere else?

Hope that helps!

/Stefan

Matthew Sundling wrote:

>I have been trying to compile an extremely simple program that
>uses a hash_map<string, int>.  I can't quit figure out what the
>errors are telling to supply to the STL container to satisfy it.
>
>Thanks for any insight you might have:
>
>[---------------------------------------------------------------]
>My program:
>
>#include <iostream>
>#include <string>
>#include <ext/hash_map>
>
>int main () {
>  std::hash_map<std::string, int > str_hash;
>  str_hash["text"] = 1;
>  std::cout << str_hash["test"] << std::endl;
>  return 0;
>}
>
>
>[---------------------------------------------------------------]
>My gcc output:
>
>/s/unsup/include/g++-v3/ext/stl_hashtable.h: In member function
>`size_t std::hashtable<_Val, _Key, _HashFcn, _ExtractKey, 
>_EqualKey, _Alloc>::_M_bkt_num_key(const _Key&, unsigned int) const 
>[with _Val = std::pair<const std::string, int>, 
>_Key = std::string,
>_HashFcn = std::hash<std::string>, 
>_ExtractKey = std::_Select1st<std::pair<const std::string, int>
>
>>, 
>>
>_EqualKey = std::equal_to<std::string>,
>_Alloc = std::allocator<int>]':
>
>/s/unsup/include/g++-v3/ext/stl_hashtable.h:512:   instantiated
>from `size_t std::hashtable<_Val, _Key, _HashFcn, _ExtractKey,
>_EqualKey, _Alloc>::_M_bkt_num(const _Val&, unsigned int) const
>[with _Val = std::pair<const std::string, int>, 
>_Key = std::string, _HashFcn = std::hash<std::string>,
>_ExtractKey = std::_Select1st<std::pair<const std::string, int>
>
>>, 
>>
>_EqualKey = std::equal_to<std::string>, 
>_Alloc = std::allocator<int>]'
>
>/s/unsup/include/g++-v3/ext/stl_hashtable.h:856:   instantiated
>from `void std::hashtable<_Val, _Key, _HashFcn, _ExtractKey,
>_EqualKey, _Alloc>::resize(unsigned int) 
>[with _Val = std::pair<const std::string, int>, 
>_Key = std::string, 
>_HashFcn = std::hash<std::string>, 
>_ExtractKey = std::_Select1st<std::pair<const std::string, int>
>
>>, 
>>
>_EqualKey = std::equal_to<std::string>, 
>_Alloc = std::allocator<int>]'
>
>/s/unsup/include/g++-v3/ext/stl_hashtable.h:669:   instantiated
>from `_Val& std::hashtable<_Val, _Key, _HashFcn, _ExtractKey,
>_EqualKey, _Alloc>::find_or_insert(const _Val&) 
>[with _Val = std::pair<const std::string, int>, 
>_Key = std::string, 
>_HashFcn = std::hash<std::string>, 
>_ExtractKey = std::_Select1st<std::pair<const std::string, int>
>
>>, 
>>
>_EqualKey = std::equal_to<std::string>, 
>_Alloc = std::allocator<int>]'
>
>/s/unsup/include/g++-v3/ext/hash_map:173:   instantiated from
>`_Tp& std::hash_map<_Key, _Tp, _HashFcn, _EqualKey,
>_Alloc>::operator[](typename std::hashtable<std::pair<const _Key,
>_Tp>, _Key, _HashFcn, std::_Select1st<std::pair<const _Key, _Tp>
>
>>, _EqualKey, _Alloc>::key_type&) 
>>
>[with _Key = std::string, 
>_Tp = int, 
>_HashFcn = std::hash<std::string>, 
>_EqualKey = std::equal_to<std::string>, 
>_Alloc = std::allocator<int>]'
>
>src/junk.cpp:7:   instantiated from here
>/s/unsup/include/g++-v3/ext/stl_hashtable.h:507: no match for
>call to `(const std::hash<std::string>) (const std::basic_string<char, 
>   std::char_traits<char>, std::allocator<char> >&)'
>
>
>Thanks!
>Matt
>
>------------------
>Matthew Sundling
>sundlm@cs.wisc.edu
>
>
>


-- 
Don't put off for tomorrow what you can do today, because if you enjoy
it today you can do it again tomorrow.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]