Title: HashTable... error: static assertion failed: st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0hash is not specialized for this type
Tags: c++;hashtable
Question: I posted earlier today and was able to work out a lot of my errors. However, I still have one error that I cannot figure out for the life of me. I'm basically just trying to insert a Symbol object into a HashTable and I'm constantly getting this message back:
In file included from /opt/local/include/gcc47/c++/bits/basic_string.h:3032:0,
from /opt/local/include/gcc47/c++/string:54,
from /opt/local/include/gcc47/c++/bits/locale_classes.h:42,
from /opt/local/include/gcc47/c++/bits/ios_base.h:43,
from /opt/local/include/gcc47/c++/ios:43,
from /opt/local/include/gcc47/c++/ostream:40,
from /opt/local/include/gcc47/c++/iostream:40,
from Driver.cpp:1:
/opt/local/include/gcc47/c++/bits/functional_hash.h: In instantiation of 'struct st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0hash<Symbol&gt;':
SeparateChaining.h:143:33:   required from 'size_t HashTable<HashedObj&gt;75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0myhash(const HashedObj&amp;) const [with HashedObj = Symbol; size_t = long unsigned int]'
SeparateChaining.h:56:51:   required from 'bool HashTable<HashedObj&gt;75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0insert(HashedObj&amp;) [with HashedObj = Symbol]'
Driver.cpp:135:26:   required from here
/opt/local/include/gcc47/c++/bits/functional_hash.h:60:7: error: static assertion failed: st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0hash is not specialized for this type
More specifically though.... The error:
error: static assertion failed: st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0hash is not specialized for this type
Here is my Driver.cpp file:
#include <iostream&gt;
#include <iomanip&gt;
#include <cassert&gt;
#include <fstream&gt;
#include <string&gt;
#include <vector&gt;
#include <time.h&gt;
#include <unistd.h&gt;
#include <map&gt;
#include <cstdlib&gt;
#include <cmath&gt;
#include "SeparateChaining.h"
using namespace std;
int TABLE_SIZE; //I know it's a global, but it allows the Table Size to be taken in within main() and used in hash()
size_t hash(const string &amp; key);
class Symbol
private:
int key;
int type;
string data;
public:
const string &amp; getData() const
return data;
int getType()
return type;
int getKey()
return labs(key);
void setType(int Type)
{
type = Type;
void setData(string Data)
data = Data;
void setKey(int Key)
key = Key;
bool operator== (const Symbol &amp; rhs) const
return getData() == rhs.getData();
bool operator!= (const Symbol &amp; rhs) const
return !(*this == rhs);
int main()
HashTable<Symbol&gt; hashtable(TABLE_SIZE);
Symbol temp;
vector<Symbol&gt; symbols;
string s;
int t;
int hash_key_array[TABLE_SIZE]; //array to hold hash key values
ifstream file;
file.open("symbols.txt");
if(!file)
cout << "System failed to open file.";
else
cout << "File successfully opened" << endl;
//for loop to read in the string name and the integer that follows the string name from symbols.txt
while(file &gt;&gt; s)
temp.setData(s);
file &gt;&gt; t;
temp.setType(t);
symbols.push_back(temp);
for(int i = 0; i < symbols.size(); i++)
cout << symbols[i].getData() << "\n";
cout << symbols[i].getType() << "\n";
cout << "What would you like the table size to be?" << endl;
cout << "Note: If the table size is greater than the number of objects" <<
" in the symbols.txt file, it will inevitably throw a segmentation fault" << endl;
cin &gt;&gt; TABLE_SIZE;
for(int j = 0; j < TABLE_SIZE; j++)
temp.setData(symbols[j].getData());
cout << temp.getData() << endl;
temp.setType(symbols[j].getType());
cout << temp.getType() << endl;
temp.setKey(75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0hash(symbols[j].getData()));
cout << "The key is: " << temp.getKey() << endl;
cout << endl;
hash_key_array[j] = temp.getKey();
for (int i = 0; i < TABLE_SIZE; i++)
{
if (i != j)
{
if (hash_key_array[i] == hash_key_array[j])
{
cout << endl;
cout << "Collision occurred at " << hash_key_array[i] << endl;
//rehash();
//cout << "The new key is: " << temp.getKey() << endl;
break;
}
}
}
hashtable.insert(temp);
size_t hash(const string &amp; key)
size_t hashVal = 0;
for(char ch : key)
hashVal = 37 * hashVal + ch;
return labs(hashVal);
And my Header File.... SeperateChaining.h:
#ifndef SEPARATE_CHAINING_H
#define SEPARATE_CHAINING_H
#include <vector&gt;
#include <list&gt;
#include <string&gt;
#include <algorithm&gt;
#include <functional&gt;
//#include "Hash.h"
using namespace std;
// SeparateChaining Hash table class
// CONSTRUCTION: an approximate initial size or default of 101
// ******************PUBLIC OPERATIONS*********************
// bool insert( x )       --&gt; Insert x
// bool remove( x )       --&gt; Remove x
// bool contains( x )     --&gt; Return true if x is present
// void makeEmpty( )      --&gt; Remove all items
int nextPrime( int n );
bool isPrime( int n );
template <typename HashedObj&gt;
class HashTable
public:
//Uses the whatever value table_size has
//Otherwise, it will make a hash table of size 101
explicit HashTable( int TABLE_SIZE )
{
currentSize = 0;
theLists.resize(TABLE_SIZE);
bool contains( const HashedObj &amp; x ) const
//represents the correct list in the hash table vector to start looking through
auto &amp; whichList = theLists[ myhash( x ) ];
//returns whatever you wanted to search for in the table provided it is there
return find( begin( whichList ), end( whichList ), x ) != end( whichList );
void makeEmpty( )
for( auto &amp; thisList : theLists )
thisList.clear( );
bool insert(HashedObj &amp; temp )
//represents the correct list in the hash table vector to start looking through
auto &amp; whichList = theLists[myhash( temp )];
//goes through the beginning and end of the list, and if it
//doesn't get to the end, then it found the object you wanted to insert in the hash table already
//prevents duplicate insertions
if( find( begin( whichList ), end( whichList ), temp ) != end( whichList) )
return false;
//otherwise, it has gotten to the end of the list without finding a duplicate
//and puts what you want to insert in the list
whichList.push_back( temp );
// Rehash; see Section 5.5
if( ++currentSize &gt; theLists.size( ) )
rehash( );
return true;
bool insert(const HashedObj &amp;&amp; x )
auto &amp; whichList = theLists[ myhash( x ) ];
if( find( begin( whichList ), end( whichList ), x ) != end( whichList ) )
return false;
whichList.push_back( st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0move( x ) );
// Rehash; see Section 5.5
if( ++currentSize &gt; theLists.size( ) )
rehash( );
return true;
bool remove( const HashedObj &amp; x )
//represents the correct list in the hash table vector to start looking through
auto &amp; whichList = theLists[ myhash( x ) ];
//trying to find x within the list
//the iterator points to the slot in the list that contains x
auto itr = find( begin( whichList ), end( whichList ), x );
//if it gets to the end of the list without finding what you want to remove, then it returns false
if( itr == end( whichList ) )
{
return false;
}
//if it finds x, it removes it from the list
whichList.erase( itr );
--currentSize;
return true;
/*
void printTable()
for(int i=0; i < symbols.size(); i++)
{
cout << "The hash table contains: " << symbols[i] << endl;
}
*/
private:
vector<list<HashedObj&gt;&gt; theLists;   // The array of Lists
int  currentSize;
void rehash( )
{
vector<list<HashedObj&gt;&gt; oldLists = theLists;
// Creates new double-sized, empty table
theLists.resize( nextPrime( 2 * theLists.size( ) ) );
for( auto &amp; thisList : theLists )
thisList.clear( );
// Copies the old table into the new table
currentSize = 0;
for( auto &amp; thisList : oldLists )
for( auto &amp; x : thisList )
insert( st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0move( x ) );
}
size_t myhash( const HashedObj &amp; x ) const
{
static hash<HashedObj&gt; hf;
return hf( x ) % theLists.size( );
}
int nextPrime( int n )
if( n % 2 == 0 )
++n;
for( ; !isPrime( n ); n += 2 )
return n;
bool isPrime( int n )
if( n == 2 || n == 3 )
return true;
if( n == 1 || n % 2 == 0 )
return false;
for( int i = 3; i * i <= n; i += 2 )
if( n % i == 0 )
return false;
return true;
#endif
I would really appreciate you guys helping me out!

Here is the accepted answer: You are doing this:
static hash<HashedObj&gt; hf;
but you have not provided a hash template, or have not specialized st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0hash for HashedObj.
You should avoid using namespace std;. Amongst other things, it is difficult to figure out which hash you want to get.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:03/18/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><hashtable>', 'Title': 'HashTable... error: static assertion failed: std::hash is not specialized for this type', 'ViewCount': 1474, 'id': 22489290, 'user_AboutMe': '<p>I write code, build things out of wood, and work on cars.</p>\n\n<p>Formerly @TimeInc, @cobrain &amp; @newbrandanalytics</p>\n\n<p>Twitter: @SwedeDeamon</p>\n\n<h1>SOreadytohelp</h1>\n', 'user_AccountId': 3202792.0, 'user_DisplayName': 'carbon_ghost', 'user_Reputation': 1096.0, 'user_Views': 188.0, 'user_id': 2704533.0}}