0

I'm making a hash table using a linked lists to handle collision. So I'm creating an array of "head" pointers to access each linked list. I can't figure out why I'm getting the error below:

error C2440: 'initializing' : cannot convert from 'LinkHash::ListNode' to 'LinkHash::ListNode *'

I commented **not working** next to the line the error is coming from.

hash.h file:

class LinkHash
{
private:
	int numItems;
	int N;
	class ListNode
	{
	public:
		int data;
		ListNode* next;
		ListNode() : next(0){};
		ListNode(int value) : data(value), next(0){};
	};
	ListNode *table, *holder;
	int hash(const int &val);
	void expand();
	void shrink();
public:
	LinkHash();
	~LinkHash();
	double loadFactor();
	void put(const int &val);
	void remove(const int &val);
	void print();
	bool found(const int &val);
};

Relevant code from .cpp file:

LinkHash::LinkHash()
{
	numItems = 0;
	N = 8;
	table = new ListNode[N];
	for(int x = 0; x < N; x++){
		table[x] = 0;
	}
}	

LinkHash::~LinkHash()
{
	delete [] table;
}

int LinkHash::hash(const int &val)
{
	return (2 * val + 5) % N;
}

bool LinkHash::found(const int &val)
{
	ListNode *temp = table[hash(val)]; //**not working**
	while(temp){
		if(temp->data == val){
			return true;
		}else{
			temp = temp->next;
		}
	}
	return false;
}

Thanks for any help

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by pandaEater
0

table is a pointer to a ListNode so you can't write table[hash(val)], you need to dereference the pointer first like this, (*table)[hash(val)]. I think that might be the issue.

Edited by SasseMan: n/a

0

table is a pointer to a ListNode so you can't write table[hash(val)], you need to dereference the pointer first like this, (*table)[hash(val)]. I think that might be the issue.

Thanks for pointing that out. I changed ListNode *table to ListNode **table. This makes table a pointer to a ListNode pointer rather than a pointer to a ListNode, which allows me to instantiate table correctly.

table = new ListNode*[N]

Now table is an array of pointers like I was intending

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.