help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

basic hash_map help required please


From: EJ
Subject: basic hash_map help required please
Date: Sat, 28 Aug 2004 14:56:19 +0100
User-agent: Mozilla Thunderbird 0.7.3 (Windows/20040803)

Hi,
I'm trying to use the hash_map STL container.

The following code works (note the preprocessor section was copied from the libstdc++ faq http://gcc.gnu.org/onlinedocs/libstdc++/faq/#5_4) :

#ifdef __GNUC__
        #if __GNUC__ < 3
                #include <hash_map.h>
                namespace Sgi { using ::hash_map; }; // inherit globals
        #else
                #include <ext/hash_map>
                #if __GNUC_MINOR__ == 0
                        namespace Sgi = std;               // GCC 3.0
                #else
                        namespace Sgi = ::__gnu_cxx;       // GCC 3.1 and later
                #endif
        #endif
#else      // ...  there are other compilers, right?
        namespace Sgi = std;
#endif
#include <iostream>
using namespace std;

int main() {
        Sgi::hash_map<int,int> my_map;
        my_map[1] = 2;
        my_map[2] = 3;
        my_map[3] = 4;
        cout << "1 -> " << my_map[1] << endl;
        cout << "2 -> " << my_map[2] << endl;
        cout << "2 -> " << my_map[3] << endl;
}

... but when I try to use a hash_map<string,string> e.g. :

#ifdef __GNUC__
        #if __GNUC__ < 3
                #include <hash_map.h>
                namespace Sgi { using ::hash_map; }; // inherit globals
        #else
                #include <ext/hash_map>
                #if __GNUC_MINOR__ == 0
                        namespace Sgi = std;               // GCC 3.0
                #else
                        namespace Sgi = ::__gnu_cxx;       // GCC 3.1 and later
                #endif
        #endif
#else      // ...  there are other compilers, right?
        namespace Sgi = std;
#endif
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
        Sgi::hash_map<string,string> my_map;
        my_map["red"] = "strawberry";
        my_map["green"] = "apple";
        my_map["yellow"] = "banana";
        cout << "red -> " << my_map["red"] << endl;
        cout << "green -> " << my_map["green"] << endl;
        cout << "yellow -> " << my_map["yellow"] << endl;
}

I get :

ejames@sonata:/data/stocks/c_progs> g++ -o hash_test2 hash_test2.cpp
/usr/include/g++/ext/stl_hashtable.h: In member function `size_t
   __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
   _Alloc>::_M_bkt_num_key(const _Key&, unsigned int) const [with _Val =
std::pair<const std::string, std::string>, _Key = std::string, _HashFcn = __gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std::pair<const std::string, std::string> >, _EqualKey = std::equal_to<std::string>, _Alloc
   = std::allocator<std::string>]':
/usr/include/g++/ext/stl_hashtable.h:519: instantiated from `size_t __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::_M_bkt_num(const _Val&, unsigned int) const [with _Val = std::pair<const std::string, std::string>, _Key = std::string, _HashFcn = __gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std::pair<const std::string, std::string> >, _EqualKey = std::equal_to<std::string>, _Alloc = std::allocator<std::string>]' /usr/include/g++/ext/stl_hashtable.h:884: instantiated from `void __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::resize(unsigned int) [with _Val = std::pair<const std::string, std::string>, _Key = std::string, _HashFcn = __gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std::pair<const std::string, std::string> >, _EqualKey = std::equal_to<std::string>, _Alloc = std::allocator<std::string>]' /usr/include/g++/ext/stl_hashtable.h:697: instantiated from `_Val& __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::find_or_insert(const _Val&) [with _Val = std::pair<const std::string, std::string>, _Key = std::string, _HashFcn = __gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std::pair<const std::string, std::string> >, _EqualKey = std::equal_to<std::string>, _Alloc = std::allocator<std::string>]' /usr/include/g++/ext/hash_map:183: instantiated from `_Tp& __gnu_cxx::hash_map<_Key, _Tp, _HashFcn, _EqualKey, _Alloc>::operator[](typename __gnu_cxx::hashtable<std::pair<const _Key, _Tp>, _Key, _HashFcn, std::_Select1st<std::pair<const _Key, _Tp> >, _EqualKey, _Alloc>::key_type&) [with _Key = std::string, _Tp = std::string, _HashFcn = __gnu_cxx::hash<std::string>, _EqualKey = std::equal_to<std::string>, _Alloc = std::allocator<std::string>]'
hash_test2.cpp:23:   instantiated from here
/usr/include/g++/ext/stl_hashtable.h:514: error: no match for call to `(const
   __gnu_cxx::hash<std::string>) (const std::basic_string<char,
   std::char_traits<char>, std::allocator<char> >&)'


output of g++ -v is :

ejames@sonata:/data/stocks/c_progs> g++ -v
Reading specs from /usr/lib/gcc-lib/i586-suse-linux/3.3.3/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --enable-languages=c,c++,f77,objc,java,ada --disable-checking --libdir=/usr/lib --enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit i586-suse-linux
Thread model: posix
gcc version 3.3.3 (SuSE Linux)


Can someone explain what extra bits and pieces I need to get a hash_map<string,string> working ?

Many thanks,
EJ


reply via email to

[Prev in Thread] Current Thread [Next in Thread]