C++ Can';不要将一个指针分配给另一个指针
我需要一些帮助来解决我遇到的错误C++ Can';不要将一个指针分配给另一个指针,c++,C++,我需要一些帮助来解决我遇到的错误 Error- C2440- 'initializing':cannot convert from 'DATAHash<ItemType>' to 'DATAHash<ItemType> *' 错误-C2440-“正在初始化”:无法从“DATAHash”转换为“DATAHash*” 我正在使用VisualStudio template<typename ItemType> class Hash { private:
Error- C2440- 'initializing':cannot convert from 'DATAHash<ItemType>' to 'DATAHash<ItemType> *'
错误-C2440-“正在初始化”:无法从“DATAHash”转换为“DATAHash*”
我正在使用VisualStudio
template<typename ItemType>
class Hash
{
private:
int tablesize;
/// Creating a Hashtable of pointer of the (class of data type)
DATAHash<ItemType>* Hashtable;
模板
类散列
{
私人:
int表大小;
///创建(数据类型类)指针的哈希表
DATAHash*哈希表;
对于Hash类,我的默认构造函数是
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;
}
模板
哈希::哈希(整数大小)
{
表大小=大小;
哈希表[size];//生成项类型的数组
对于(int i=0;i
这就是我的错误所在
/// 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;
}
///这是add函数
///它添加作为参数传递的数据
///进入哈希表
模板
void Hash::additem(ItemType数据)
{
DATAHash*newdata=newdatahash(数据);
///获取数据作为参数,并调用哈希函数来创建地址
int index=Hashfunction(data->getCrn());
///检查该索引中是否已有数据或没有数据。
DATAHash*temptr=Hashtable[index];next!=nullptr)
当前=当前->下一步;
当前->下一步=新数据;
cout您需要像这样获取指针:
DATAHash<ItemType> * tempptr = &Hashtable[index];
DATAHash*temptr=&Hashtable[index];
但是,我不确定这是否是您应该做的。您正在该指针上调用[]
运算符,但从未为其分配任何内存
哈希表
成员是如何初始化的?因此,当您按原样获取某事物的索引时,哈希表[index]
,您会得到一个实际值。DATAHash*
是指针类型,因此它包含一个地址
我猜您要寻找的解决方案是使用哈希表[index]
的地址,因此您需要使用以下代码修复该行:
DATAHash*temptr=&Hashtable[index];
您说“无法将指针分配给另一个指针”,但错误消息清楚地表明您正在尝试将非指针分配给指针变量。您甚至会注释“make array of item type”指示它实际上是一个项类型的数组,而不是指针数组。Hashtable[size];//生成项类型的数组
nope