error::'item' does not name a type

i have this problem (error::'item' does not name a type) and i cant figure it out.If you can help me i would appriciate it.Thanks in advance.


#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <iostream>
#include <string>

using namespace std;


class HashTable
{
public:
HashTable();
void Insert(int d);
int Hash(int data);
int GetSize();
bool SearchNumber(int key);

protected:

private:
void Expand();
int tablesize = 10;
item *hashtable = new item[tablesize];
struct item
{
int data;
item *next;
};


};

#endif // HASHTABLE_H
//Constuctor
HashTable::HashTable()
{
for (int i=0;i<tablesize;i++)
{
hashtable[i] = new item;
hashtable[i]->data = -1;
hashtable[i]->next = NULL;
}
}

//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;
}
1
2
3
4
5
6
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.
Last edited on
In addition:
item *hashtable = new item[tablesize];
You're making an array of item objects here.

But then...
1
2
3
hashtable[i] = new item;
hashtable[i]->data = -1;
hashtable[i]->next = NULL;

You try to access an item object (hashtable[i]) as if it were a pointer. It's not a pointer.
You're mixing objects and pointers around.

If you want to create an array of pointers, you need to do item** hashtable = new item*[tablesize];.
Last edited on
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.
Ganado.

If i do that (item** hashtable = new item*[tablesize];) then i wont have to change anything from my current program?.Thanks in advance.
Last edited on
I can't say for sure, why not try it out and see what happens? Experiment and learn.
Topic archived. No new replies allowed.