2014-12-07 59 views
0

*** UPDATE ****在指針數組中的鏈表中創建對象時編譯錯誤:C++

所以開始我試圖嘗試哈希。爲了儘量縮短它,我創建了一個需要一個通用參數的linkedlsit類。我有一個哈希表類,我試圖創建(我相信)鏈表指針(牢記的LinkedList需要一個通用型)

所以,在我的哈希表類,我有一個私有變量的陣列,使得

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

它的工作原理如果我將條目替換爲鏈接中的對象edlist作爲jsut一個int,或浮動,甚至字符串

所以,這可能是什麼原因造成的?請和謝謝你,如果你需要任何更多的信息,讓我知道:)

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

是否'SLL '有一個成員函數,看起來像'追加(T *)'?我認爲你需要努力縮小與此相關的代碼(並更新你的問題),否則幾乎無法繼續。 – cartographer 2014-12-07 03:47:25

+0

更新了SLL類除了幾個函數,所以我沒有附加(T *)即時猜測,類(對象)需要作爲指針傳遞,因此T *部分? – 2014-12-07 05:45:55

回答

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),並沒有定義這樣的功能!

這是一些應該爲您編譯的最低工作代碼。請注意,爲簡潔起見,我使用了而不是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; 
} 
+0

張貼在這裏之前,我試圖 '進入溫度( 「測試」,0);' '名單[散列] - >追加(TEMP);' ,給了我同樣的錯誤,但我可以再次嘗試時我上我的電腦 – 2014-12-07 07:25:04

+0

@TannerSummers我已經更新了我的答案,包括一些示例代碼。 – cartographer 2014-12-07 16:39:34