在指针数组中的链表中创建对象时编译错误:C ++

时间:2014-12-07 02:07:23

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

*** **** UPDATE

所以开始我试图尝试散列。为了简化它,我创建了一个linklsit类,它接受一个泛型参数。我有一个哈希表类,我试图创建(我相信)一组链表列表指针(请记住链表是一个泛型类型)

所以,在我的哈希表类中,我有一个私有变量

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或甚至是字符串

那么是什么导致了这个?拜托,谢谢,如果您需要更多信息,请告诉我们:)

#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);;
 }

1 个答案:

答案 0 :(得分:0)

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

首先,您的班级SLL<T> 声明许多成员函数, 其中两个是SLL::append(T&)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;
}
相关问题