【发布时间】:2026-01-10 09:20:03
【问题描述】:
***更新****
所以我开始尝试散列。为了简短起见,我创建了一个带有泛型参数的linkedlsit 类。我有一个哈希表类,我试图在其中创建(我相信)一个链表指针数组(记住链表采用泛型类型)
所以,在我的哈希表类中,我有一个私有变量
SLL< Entry <string, int> >** list;
其中 SLL 是我的链表,Entry 是包含键(字符串)和值(int)并绑定使其成为指针数组的对象。
在哈希表构造函数中我这样创建它
list = new SLL<Entry<string, int> > * [this->size];
现在在我的代码中,我尝试在哈希码函数结束后将 Entry 对象附加到数组中
list[hash]->append(new Entry<string, int>(key, e));
但它得到了这个错误
HashTable.h: In member function 'void HashTable::createEntry(std::string, int)':
HashTable.h:78:53: error: no matching function for call to 'SLL<Entry<std::basic_string<char>, int> >::append(Entry<std::basic_string<char>, int>*)'
list[hash]->append(new Entry<string, int>(key, i));
如果我将 Entry 作为链表中的对象替换为 jsut 一个 int、float 或什至是 string,它会起作用
那是什么原因造成的呢?拜托,谢谢,如果您需要更多信息,请告诉我:)
#ifndef SLL_H
#define SLL_H
template <class T>
class SLL
{
private:
Node<T>* head;
Node<T>* tail;
int size;
public:
SLL();
virtual ~SLL();
void append(T&);
void append(T*);
void prepend(T);
void deleteElem(int);
void toString();
int getSize();
void insertAt(T, int);
T retrieveDataAt(int);
};
#endif /* SLL_H */
template <class T>
SLL<T>::SLL()
{
this->tail = NULL;
this->head = NULL;
this->size = 0;
}
void SLL<T>::append(T data)
{
//do stuff
this->head = new Node<T>(data);;
}
【问题讨论】:
-
SLL<T>是否有一个看起来像append(T*)的成员函数?我认为您需要尝试缩小涉及此的代码(并更新您的问题),否则几乎没有什么可做的。 -
更新了 SLL 类,不包括一些函数,所以我没有 append(T*) 我猜,类(对象)需要作为指针传递,因此 T* 部分?
标签: c++ templates hash linked-list hashtable