ambigous symbol
Nov 24, 2012 at 8:56pm UTC
What does that mean?
error C2872: 'hash' : ambiguous symbol could be 'hash' or c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(763) : std::hash'
Here is my code.
Hash_table.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
template <typename HashedObj>
class HashTable
{
public :
explicit HashTable( int size = 101 ) : array( nextPrime( size ) )
{ makeEmpty( ); }
bool contains( const HashedObj & x ) const
{
return isActive( findPos( x ) );
}
void makeEmpty( )
{
currentSize = 0;
for ( size_t i = 0; i < array.size( ); i++ )
array[ i ].info = EMPTY;
}
bool insert( const HashedObj & x )
{
// Insert x as active
unsigned currentPos = findPos( x );
if ( isActive( currentPos ) )
return false ;
array[ currentPos ] = HashEntry( x, ACTIVE );
// Rehash; see Section 5.5
if ( ++currentSize > array.size( ) / 2 )
rehash( );
return true ;
}
bool remove( const HashedObj & x )
{
int currentPos = findPos( x );
if ( !isActive( currentPos ) )
return false ;
array[ currentPos ].info = DELETED;
return true ;
}
enum EntryType { ACTIVE, EMPTY, DELETED };
private :
struct HashEntry
{
HashedObj element;
EntryType info;
HashEntry( const HashedObj & e = HashedObj( ), EntryType i = EMPTY )
: element( e ), info( i ) { }
};
vector<HashEntry> array;
unsigned currentSize;
bool isActive( int currentPos ) const
{ return array[ currentPos ].info == ACTIVE; }
int findPos( const HashedObj & x ) const
{
int offset = 1;
unsigned currentPos = myhash( x );
while ( array[ currentPos ].info != EMPTY &&
array[ currentPos ].element != x )
{
currentPos += offset; // Compute ith probe
offset += 2;
if ( currentPos >= array.size( ) )
currentPos -= array.size( );
}
return currentPos;
}
void rehash( )
{
vector<HashEntry> oldArray = array;
// Create new double-sized, empty table
array.resize( nextPrime( 2 * oldArray.size( ) ) );
for ( unsigned j = 0; j < array.size( ); j++ )
array[ j ].info = EMPTY;
// Copy table over
currentSize = 0;
for ( unsigned i = 0; i < oldArray.size( ); i++ )
if ( oldArray[ i ].info == ACTIVE )
insert( oldArray[ i ].element );
}
int myhash( const HashedObj & x ) const
{
int hashVal = hash( x );
hashVal %= array.size( );
if ( hashVal < 0 )
hashVal += array.size( );
return hashVal;
}
};
int hash( int key )
{
return key;
}
/**
* A hash routine for string objects.
*/
template <typename HashedObj>
int hash( const HashedObj & key )
{
int hashVal = 0;
for ( unsigned i = 0; i < key.length( ); i++ )
hashVal = 37 * hashVal + key[ i ];
return hashVal;
}
#endif
Nov 24, 2012 at 9:02pm UTC
you have int hash
on line 109 and 119.
Nov 24, 2012 at 9:09pm UTC
It has to overload this functions. I do not think that error is about it. Anyway, I tried to delete one of them but nothing changes. Still get same error.
Nov 24, 2012 at 9:18pm UTC
This can easily happen when you use using namespace std;
. The compiler doesn't know if you mean std::hash or your hash function.
Last edited on Nov 24, 2012 at 9:18pm UTC
Nov 24, 2012 at 9:23pm UTC
I always use using namespace std
;, if I change it I need to correct lots of thing by using std::
everywhere. Isn't there anyway to fix this without removing using namespace std;
?
Nov 24, 2012 at 9:34pm UTC
You could refer to your hash function as ::hash.
int hashVal = ::hash( x );
.. or use a different function name.
Last edited on Nov 24, 2012 at 9:38pm UTC
Nov 24, 2012 at 9:53pm UTC
Thank you very much, when I add it, it is solved.
Topic archived. No new replies allowed.