1,105,406 Community Members

hash function error when using gcc

Member Avatar
ktob
Newbie Poster
2 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi,
this code compiled using visual studio. Have changed the line
const size_t i= __gnu_cxx::hash(x);
from

const size_t i=stdext::hash_value(x);

so that it would compile using gcc. However I am getting an error message as shown below.
Any suggestions appreciated. Thx

template<class X, class Pred= less<X> >
class Hash{
private:
	Pred comp;
public:
	enum{bucket_size = 4, min_buckets = 8};	
	Hash() : comp(){ }
	Hash(Pred p) : comp(p){ }
	size_t operator()(const X& x) const{
		const size_t i= __gnu_cxx::hash(x);
		return 16807U*(i%127773U)+(95329304U-2836U*(i/127773U));
	}
	
};

error: expected initializer before __gnu_cxx

Member Avatar
vijayan121
Posting Virtuoso
1,769 posts since Dec 2006
Reputation Points: 1,097 [?]
Q&As Helped to Solve: 329 [?]
Skill Endorsements: 16 [?]
 
0
 

ideally, do not use the (now deprecated) functionality in namespace __gnu_cxx . they have been superceded by tr1 in c++0x.
use -std=c++0x (gcc 4.3) , std::tr1 (gcc4.2) or boost::tr1 (earlier versions).

the error is because __gnu_cxx::hash<> is a struct (function object); not a function. if you have to use it, use it this way

#include <functional>
#include <ext/hash_fun.h>
using namespace std ;

template<class X, class Pred= less<X> >
class Hash{
private:
  Pred comp;
public:
  enum{bucket_size = 4, min_buckets = 8}; 
  Hash() : comp(){ }
  Hash(Pred p) : comp(p){ }
  size_t operator()(const X& x) const{
    const size_t i= __gnu_cxx::hash<X> [B]()[/B] (x);
    return 16807U*(i%127773U)+(95329304U-2836U*(i/127773U));
  }
  
};
Member Avatar
ktob
Newbie Poster
2 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you thank you!! That worked.

Question Answered as of 5 Years Ago by vijayan121
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article