[英]C++ - can't assign top-const pointer to another non-const pointer
[英]Can't assign a pointer to another pointer
我需要一些帮助来解决我遇到的错误。
Error- C2440- 'initializing':cannot convert from 'DATAHash<ItemType>' to 'DATAHash<ItemType> *'
我正在使用Visual Studio。
template<typename ItemType>
class Hash
{
private:
int tablesize;
/// Creating a Hashtable of pointer of the (class of data type)
DATAHash<ItemType>* Hashtable;
对于哈希类,我的默认构造函数是
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 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;
}
这是我第一次发布问题,所以,如果还有其他需要发布的信息,请告诉我。
谢谢您的帮助。
-雷兹
您需要这样获得指针:
DATAHash<ItemType> * tempptr = &Hashtable[index];
但是,我不确定这是您应该做什么。 您正在该指针上调用[]
运算符,但从未为其分配任何内存。
Hashtable
成员如何初始化?
因此,当您像Hashtable[index]
获取某物的Hashtable[index]
,您会得到一个实际值。 DATAHash<ItemType>*
是指针类型,因此它包含一个地址。
我猜想您要寻找的解决方案是采用Hashtable[index]
的地址,因此您需要使用以下方法修复该行:
DATAHash<ItemType> * tempptr = &Hashtable[index];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.