//Hashing the number
int HashTable::Hash(int data)
{
return (data%tablesize);
}
//Inserts a new item to the HashTable
void HashTable::Insert(int d)
{
int index = Hash(d);
if(hashtable[index]->data == -1)
{
hashtable[index]->data = d;
}
else
{
item *pointer = hashtable[index];
item *New = new item;
New->data = -1;
New->next = NULL;
while(pointer != NULL)
{
pointer = pointer->next;
}
pointer->next = New;
}
}
//Gets the number of items in HashTable
int HashTable::GetSize()
{
int counter = 0;
for(int i=0;i<tablesize;i++)
{
if(hashtable[i]->data != -1)
{
counter++;
item *pointer = hashtable[i];
while(pointer->next != NULL)
{
counter++;
pointer = pointer->next;
}
}
}
return counter;
}
//Finds the key if it exists and returns true else it returns false
bool HashTable::SearchNumber(int key)
{
bool flag = false;
int index = Hash(key);
item *pointer = hashtable[index];
while(pointer->data != -1)
{
if (pointer->data == key)
{
flag = true;
}
pointer = pointer->next;
}
return flag;
}
item *hashtable = new item[tablesize];
struct item
{
int data;
item *next;
};
You're trying to create an object of type item before the compiler knows what it is. First show compiler what it is, THEN use it.
1 2 3 4 5 6 7
struct item
{
int data;
item *next;
};
item *hashtable = new item[tablesize];
Also, DON'T USE NEW. DON'T DO MANUAL MEMORY MANAGEMENT. C++ has had good smart pointers since 2011.
Does HashTable.cpp have #include HashTable.h at the top? Your code doesn't show that.
Defining item inside the HashTable class isn't inherently wrong, but it seem that you're limiting the HashTable to being able to support only one kind of object.
Thanks for the reply,ive found the solution a few mins ago.Also i'm planning to create a function that expands the hashtable and i'm fine with supporting one kind of object,because thats what i need for my project.