Runtime error in hash table

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:
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
#ifndef HTABLE_H
#define HTABLE_H


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <cassert>
#include <algorithm>

using namespace std;

//template <class T>
class HTable
{
public:
	HTable();
	HTable(const int &);
	~HTable();
	bool contains(const string &);
	int hash(const string &);
	void insert(const string &);
	void remove(string &);
	void print();
private:
	vector<list<string>> ht;
	int TABLESIZE;
	int key;
	int currentSize;

};

inline HTable::HTable()
{
	TABLESIZE=33;
	vector<list<string>> ht (TABLESIZE);
	currentSize = 0;
}
//template <class T>
inline HTable::HTable(const int &size)
{
	TABLESIZE = size;
	vector<list<string>> ht (TABLESIZE);
	
	currentSize = 0;
}

I want to mention that I have tried creating a pointer for the vector such as:
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
#ifndef HTABLE_H
#define HTABLE_H


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <cassert>
#include <algorithm>

using namespace std;

//template <class T>
class HTable
{
public:
	HTable();
	HTable(const int &);
	~HTable();
	bool contains(const string &);
	int hash(const string &);
	void insert(const string &);
	void remove(string &);
	void print();
private:
	vector<list<string>> *ht;
	int TABLESIZE;
	int key;
	int currentSize;

};

inline HTable::HTable()
{
	TABLESIZE=33;
	ht = new vector<list<string>>(TABLESIZE);
	currentSize = 0;
}
//template <class T>
inline HTable::HTable(const int &size)
{
	TABLESIZE = size;
	ht = new vector<list<string>>(TABLESIZE);
	
	currentSize = 0;
}
and it is giving me some sort of compilation error saying I cannot convert a type string to type list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
inline HTable::HTable()
{
	TABLESIZE=33;
	vector<list<string>> ht (TABLESIZE);
	currentSize = 0;
}

inline HTable::HTable(const int &size)
{
	TABLESIZE = size;
	vector<list<string>> ht (TABLESIZE);
	
	currentSize = 0;
}


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
const int defaultTableSize = 33 ;

inline HTable::HTable() 
    : ht(defaultTableSize), TABLESIZE(defaultTableSize), key(/* ? */), currentSize(0)
{
}

inline HTable::HTable(const int &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.]
Last edited on
Topic archived. No new replies allowed.