I need some help with the troubleshooting the error i'm getting.
Error- C2440- 'initializing':cannot convert from 'DATAHash<ItemType>' to 'DATAHash<ItemType> *'
I'm using Visual Studio.
template<typename ItemType>
class Hash
{
private:
int tablesize;
/// Creating a Hashtable of pointer of the (class of data type)
DATAHash<ItemType>* Hashtable;
and for Hash class, my default constructor is
template<typename ItemType>
Hash<ItemType> ::Hash(int size)
{
tablesize = size;
Hashtable[size]; // make array of item type
for (int i = 0; i< size; i++)
Hashtable[i] = NULL;
loadfactor = 0;
}
This is where my error is
/// This is add function
/// It adds the data that is passed as a parameter
/// into the Hash table
template<typename ItemType>
void Hash<ItemType>::additem(ItemType data)
{
DATAHash<ItemType> * newdata = new DATAHash<ItemType>(data);
/// gets the data as a parameter and calls Hash function to create an address
int index = Hashfunction(data->getCrn());
/// Checking if there if there is already a data in that index or no.
DATAHash<ItemType> * tempptr = Hashtable[index]; <------- Error line
// there is something at that index
// update the pointer on the item that is at that index
if (tempptr != nullptr)
{
// walk to the end of the list and put insert it there
DATAHash<ItemType> * current = tempptr;
while (current->next != nullptr)
current = current->next;
current->next = newdata;
cout << "collision index: " << index << "\n";
return;
}
This is my first time posting a question so, if there's something else i need to post, Let me know.
Thanks for the help.
-Rez
You need to get the pointer like this:
DATAHash<ItemType> * tempptr = &Hashtable[index];
However I am not really sure this is what you should be doing. You are invoking the []
operator on that pointer but you never allocate any memory for it.
How the Hashtable
member is initialized?
So when you take the index of something as you are Hashtable[index]
, you get an actual value back. DATAHash<ItemType>*
is of type pointer, so it holds an address.
I would guess the solution you would be looking for is taking the address of Hashtable[index]
, so you would need to fix that line with:
DATAHash<ItemType> * tempptr = &Hashtable[index];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.