[英]Can't assign a pointer to another pointer
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. 我正在使用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? Hashtable
成员如何初始化?
So when you take the index of something as you are Hashtable[index]
, you get an actual value back. 因此,当您像Hashtable[index]
获取某物的Hashtable[index]
,您会得到一个实际值。 DATAHash<ItemType>*
is of type pointer, so it holds an address. DATAHash<ItemType>*
是指针类型,因此它包含一个地址。
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: 我猜想您要寻找的解决方案是采用Hashtable[index]
的地址,因此您需要使用以下方法修复该行:
DATAHash<ItemType> * tempptr = &Hashtable[index];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.