I am getting a strange runtime error when trying to run my hash table program. I have it separated in 3 separate files. I believe I have narrowed down where the error is occurring. It is either happening in insert(const &string s) or in the implementation of vector<list<string>> ht. I would appreciate some input. This is the implementation of insert():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void HTable::insert(const string &s)
{
int h = hash(s);
cout<<h<<endl;
list<string> &tempList = ht[h];
if( find( tempList.begin( ), tempList.end( ), s ) == tempList.end( ) )
{
tempList.push_back( s );
currentSize++;
}
cout<<currentSize<<endl;
}
and this is the declaration and implementation of my vector:
In these constructors you create a local vector ht that hides the member variable ht. The local variable ceases to exist when the constructor is finished executing and the member variable is just an empty vector, so any time you attempt to access an element of the vector, you're going to invoke undefined behavior.
Prefer to use constructor initialization lists.
1 2 3 4 5 6 7 8 9 10 11
constint defaultTableSize = 33 ;
inline HTable::HTable()
: ht(defaultTableSize), TABLESIZE(defaultTableSize), key(/* ? */), currentSize(0)
{
}
inline HTable::HTable(constint &size) // why are you passing an int by const reference?
: ht(size), TABLESIZE(size), key( /* ? */ ), currentSize(0)
{
}
[edit: I don't see much point in having a member TABLESIZE. just use ht.size() to query the size of the table.]