#include "SeparateChaining.h"
#include <iostream>
usingnamespace std;
/**
* Internal method to test if a positive number is prime.
* Not an efficient algorithm.
*/
bool isPrime( int n )
{
if( n == 2 || n == 3 )
returntrue;
if( n == 1 || n % 2 == 0 )
returnfalse;
for( int i = 3; i * i <= n; i += 2 )
if( n % i == 0 )
returnfalse;
returntrue;
}
/**
* Internal method to return a prime number at least as large as n.
* Assumes n > 0.
*/
int nextPrime( int n )
{
if( n % 2 == 0 )
n++;
for( ; !isPrime( n ); n += 2 )
;
return n;
}
/**
* A hash routine for string objects.
*/
int hash( const string & key )
{
int hashVal = 0;
for( int i = 0; i < key.length( ); i++ )
hashVal = 37 * hashVal + key[ i ];
return hashVal;
}
/**
* A hash routine for ints.
*/
int hash( int key )
{
return key;
}
#include <iostream>
#include "SeparateChaining.h"
usingnamespace std;
// Simple main
int main( )
{
HashTable<int> H;
constint NUMS = 4000;
constint GAP = 37;
int i;
cout << "Checking... (no more output means success)" << endl;
for( i = GAP; i != 0; i = ( i + GAP ) % NUMS )
H.insert( i );
for( i = 1; i < NUMS; i += 2 )
H.remove( i );
for( i = 2; i < NUMS; i += 2 )
if( !H.contains( i ) )
cout << "Contains fails " << i << endl;
for( i = 1; i < NUMS; i += 2 )
{
if( H.contains( i ) )
cout << "OOPS!!! " << i << endl;
}
return 0;
}
This looks like some code that an old version of VC++ let by, when it shouldn't have.
I think it needs typename at the start of lines 61 and 88 in SeparateChaining.h, and in that same header file is an attempt on line 96 to use a function that has the prototype after that line. I also think there are some issues with const to non-const going on.
Sorry I didnt include the error message. I assumed you guy would probably compile it but any ways here it is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
SeparateChaining.h: In member function `bool HashTable<HashedObj>::remove(const HashedObj&)':
SeparateChaining.h:61: error: expected `;' before "itr"
SeparateChaining.h:63: error: `itr' undeclared (first use this function)
SeparateChaining.h:63: error: (Each undeclared identifier is reported only once for each function it appears in.)
SeparateChaining.h: In member function `void HashTable<HashedObj>::rehash()':
SeparateChaining.h:88: error: expected `;' before "itr"
SeparateChaining.h:89: error: `itr' undeclared (first use this function)
SeparateChaining.h: In member function `bool HashTable<HashedObj>::remove(const HashedObj&) [with HashedObj = int]':
TestSeparateChaining.cpp:19: instantiated from here
SeparateChaining.h:61: error: dependent-name ` std::list<HashedObj,std::allocator<_CharT> >::iterator' is parsed as a non-type, but instantiation yields a type
SeparateChaining.h:61: note: say `typename std::list<HashedObj,std::allocator<_CharT> >::iterator' if a type is meant
SeparateChaining.h: In member function `void HashTable<HashedObj>::rehash() [with HashedObj = int]':
SeparateChaining.h:53: instantiated from `bool HashTable<HashedObj>::insert(const HashedObj&) [with HashedObj = int]'
TestSeparateChaining.cpp:17: instantiated from here
SeparateChaining.h:88: error: dependent-name ` std::list<HashedObj,std::allocator<_CharT> >::iterator' is parsed as a non-type, but instantiation yields a type
SeparateChaining.h:88: note: say `typename std::list<HashedObj,std::allocator<_CharT> >::iterator' if a type is meant
I used Microsoft visual studio 2010 and got the same errors so I do not believe its my compiler.
Older MS compiler were much less.... formal when dealing with this sort of thing. They would let all sorts of wrong things through, particularly in the field of iterators. As I said above, stick typename at the start of line 61 and 88, and many of those errors will vanish.