【问题标题】:Compile error when creating object within linkedlist within pointer array: C++在指针数组内的链表内创建对象时编译错误:C++
【发布时间】: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&lt;T&gt; 是否有一个看起来像append(T*) 的成员函数?我认为您需要尝试缩小涉及此的代码(并更新您的问题),否则几乎没有什么可做的。
  • 更新了 SLL 类,不包括一些函数,所以我没有 append(T*) 我猜,类(对象)需要作为指针传递,因此 T* 部分?

标签: c++ templates hash linked-list hashtable


【解决方案1】:

您发布的代码存在一些问题,它只是显示 使用模板时,您需要确保一切都很好地匹配。 特别是因为编译器甚至不会关心某些类型的错误 直到您实际实例化具有某种类型的模板。

首先是你的类SLL&lt;T&gt;声明了一些成员函数, 其中两个是SLL::append(T&amp;)SLL::append(T*)。问题是在 您发布的示例代码,您正在定义的成员函数是 SLL::append(T),不存在!

第二个是因为new返回一个指向类型的指针,你的代码:

list[hash]->append(new Entry<string, int>(key, e));

等价于

Entry<string, int>* data_ptr = new Entry<string, int>(key, e);
list[hash]->append(data_ptr);

它将寻找SLL::append(T*) 形式的成员函数,而不是 SLL::append(T),还没有定义这个函数!

这里有一些应该为您编译的最低限度的工作代码。请注意,我 使用 std::pair 而不是 Entry 为简洁起见,您需要使用 -std=c++11 或等效标志(例如g++ -std=c++11 main.cpp),因为我使用了nullptr

#include <utility>
#include <string>

template<class T>
class SLL;

// singly linked list node
template<class T>
class Node
{
private:
    Node<T> *next;
    T data;

    friend class SLL<T>;
public:
    Node(T input) : next(nullptr),
        data(input) {}
    ~Node() {delete next;}
};

// the singly linked list class
template <class T>
class SLL
{
private:
    Node<T>* head;
    Node<T>* tail;
    std::size_t size;

public:
    SLL() : head(nullptr),
        tail(nullptr), size(0) {}
    ~SLL() {delete head;}

    std::size_t getSize() const {
        return size;}
    void append(T data);
};

template<class T>
void SLL<T>::append(T data)
{
    Node<T> *temp = new Node<T>(data);
    if (!head)
        head = temp;

    if (tail)
        tail->next = temp;
    tail = temp;

    size += 1;
}

int main()
{
    // less typing
    using list_type = SLL<std::pair<std::string, int>>;

    // allocation for the list of lists
    std::size_t hash_size = 10;
    list_type** list_of_lists = new list_type*[hash_size]();

    // data to input
    std::string key = "key";
    int value = 9330323;

    std::size_t hash = 4;

    // check and append
    if (!list_of_lists[hash])
        list_of_lists[hash] = new list_type;

    list_of_lists[hash]->append(std::pair<std::string, int>(key, value));

    // cleanup
    for (std::size_t i = 0; i < hash_size; ++i)
        delete list_of_lists[i];
    delete[] list_of_lists;
}

【讨论】:

  • 在发布之前我尝试了Entry&lt;string, int&gt; temp("test", 0); list[hash]-&gt;append(temp); 并给了我同样的错误,但我可以在我的电脑上再试一次
  • @TannerSummers 我已经更新了我的答案,包括一些示例代码。