The following code works with g++ 2.75 but
i've some troubles with compiling with g++ 3.0.
Thanks in advance if you have a solution.
Here is the error message.
main.hpp:28: specializing `struct std::hash<std::string>' in different
namespace
/usr/include/g++-v3/ext/stl_hash_fun.h:68: from definition of `template<class
_Key> struct std::hash'
make: *** [resource_identifier.lo] Error 1
#include <pair.h>
#include <string>
#include <map>
#include <vector>
#ifdef __GNUC__ /* grabbed in libstdc++ faq */
#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
using namespace std;
//TODO: include this definition in the Hashtable class
template<> struct hash<string>
{
size_t operator()(const string& s) const
{
return hash<char const*>()(s.c_str());
}
};
#ifdef NAMESPACES
namespace foobar {
#endif
class NoSuchElementException
{
public:
NoSuchElementException(string w) {_what = w;}
virtual ~NoSuchElementException() {}
virtual string what() { return _what; }
private:
string _what;
};
template <class T> class Hashtable //: public ost::Conditional
{
public:
Hashtable() {} //: Conditional() {}
virtual ~Hashtable() {clear(); }
virtual T put(const string& key, T value)
{
assert(key.c_str() != NULL); //TODO: must throw ?
// enterMutex();
T returnValue = mHashtable[key];
mHashtable[key] = value;
// leaveMutex();
return returnValue;
}
virtual T get(const string& key) throw (NoSuchElementException)
{
assert(key.c_str() != NULL); //TODO: must throw ?
// enterMutex();
if (mHashtable.empty())
{
// leaveMutex ();
throw NoSuchElementException (key);
}
if (mHashtable.find(key) == mHashtable.end())
{
// leaveMutex();
throw NoSuchElementException(key);
}
T returnValue = mHashtable[key];
//leaveMutex();
return returnValue;
}
virtual void del(const string& key) throw (NoSuchElementException)
{
assert(key.c_str() != NULL); //TODO: must throw ?
// enterMutex();
if (mHashtable.find(key) == mHashtable.end()) {
// leaveMutex();
throw NoSuchElementException(key);
}
mHashtable.erase (key);
// leaveMutex();
return;
}
virtual void clear() {
// enterMutex();
mHashtable.erase(mHashtable.begin(), mHashtable.end());
// leaveMutex();
}
virtual bool isEmpty () {
bool ret;
// enterMutex();
ret = mHashtable.empty ();
// leaveMutex();
return ret;
}
virtual vector<pair<string,T> > toVector()
throw (NoSuchElementException)
{
vector <pair<string, T> > v;
// enterMutex();
if (mHashtable.empty()) {
// leaveMutex();
throw NoSuchElementException(string("hashtable is empty"));
}
for(hash_map<string, T , hash<string> >::iterator iter
= mHashtable.begin(); iter != mHashtable.end(); ++iter)
{
pair <string, T> p(*iter);// (iter->first, iter->second);
v.insert(v.begin(), p);
}
//leaveMutex();
return v;
}
protected:
hash_map<string, T, hash<string> > mHashtable;
};
int
main(void)
{
Hashtable<int> h;
return 0;
}
#ifdef NAMESPACES
}; // foobar
#endif
/** EMACS **
* Local variables:
* mode: c++
* c-basic-offset: 4
* End:
*/
Here is my release of g++ 3.0:
$ g++-3.0 -v
Reading specs from /usr/lib/gcc-lib/i386-linux/3.0.4/specs
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f77,proto,objc
--prefix=/usr --infodir=/share/info --mandir=/share/mx
Thread model: posix
gcc version 3.0.4
Here is my release of g++ 2.95:
g++ -v
Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.4/specs
gcc version 2.95.4 20011002 (Debian prerelease)
--
Lu*********@cvf.fr