2012-04-15 20 views
0

我正在C++中使用類模板實現有序列表數據結構。爲了簡單起見,我實現了每個構造函數和函數內聯。我爲這個項目創建了自己的Node類。類模板 - 對我的Node類的解構器的未定義引用。

編譯器錯誤被粘貼在這個問題的底部。 「未定義對`Node ::〜Node()'的引用」。這是我第一次使用模板,我從來沒有見過這個錯誤。我不知道從哪裏開始。 任何幫助,將不勝感激!

Node.h

#ifndef NODE_H 
#define NODE_H 

#include <iostream> 
#include <cstdlib> 

template <class E> 
class Node { 
public: 
    Node(const E init_data = NULL, Node<E>* init_link = NULL){data = init_data; link = init_link;} 
    Node(const Node<E>& orig){data = orig.getData(); setLink = NULL;} 
    virtual ~Node(); 

    E getData() const{return data;} 
    void setData(E newData){data = newData;} 

    Node<E>* getLink(){return link;} 
    void setLink(Node<E>* nextLink) {link = nextLink;} 
private: 
    E data; 
    Node<E>* link; 
}; 

#endif /* NODE_H */ 

MyOrderedList.h

#ifndef MYORDEREDLIST_H 
#define MYORDEREDLIST_H 

#include <iostream> 
#include <cstdlib> 
#include "Node.h" 

template <class E> 
class MyOrderedList; 
template <class E> 
std::ostream& operator <<(std::ostream& out, const MyOrderedList<E>& list); 

template <class E> 
class MyOrderedList { 
public: 
    MyOrderedList() 
    {/*IMPLEMENTATION*/} 

    MyOrderedList(const MyOrderedList<E>& orig) 
    {/*IMPLEMENTATION*/} 

    void operator =(const MyOrderedList<E>& orig) 
    {/*IMPLEMENTATION*/} 

    virtual ~MyOrderedList() 
    {/*IMPLEMENTATION*/} 

    bool remove(E data) 
    {/*IMPLEMENTATION*/} 

    MyOrderedList<E> kLargest(int k) const 
    {/*IMPLEMENTATION*/} 

    E get(int pos) const 
    {/*IMPLEMENTATION*/} 

    void insert(E data) 
    {/*IMPLEMENTATION*/} 

    MyOrderedList<E> operator +(const MyOrderedList<E>& list) 
    {/*IMPLEMENTATION*/} 

    friend std::ostream& operator <<(std::ostream& out, const MyOrderedList<E>& list) 
    {/*IMPLEMENTATION*/} 

private: 
    Node<E>* head; 
    int size; 
}; 

#endif //MYORDEREDLIST_H 

的main.cpp

#include <cstdlib> 
#include <iostream> 
#include "MyOrderedList.h" 

using namespace std; 

int main(int argc, char** argv) 
{ 
    MyOrderedList<int> list; 
    list.insert(5); 
    std::cout << list << std::endl;; 

    return 0; 
} 

編譯器錯誤

g++  -o dist/Debug/Cygwin-Windows/project7_windows build/Debug/Cygwin-Windows/main.o 
build/Debug/Cygwin-Windows/main.o: In function `_ZN4NodeIiE7getLinkEv': 
/cygdrive/c/Users/John/Desktop/Dropbox/Data Structures/Project7 Windows/MyOrderedList.h:(.rdata$_ZTV4NodeIiE[vtable for Node<int>]+0x8): undefined reference to `Node<int>::~Node()' 
/cygdrive/c/Users/John/Desktop/Dropbox/Data Structures/Project7 Windows/MyOrderedList.h:(.rdata$_ZTV4NodeIiE[vtable for Node<int>]+0xc): undefined reference to `Node<int>::~Node()' 
collect2: ld returned 1 exit status 
make[2]: *** [dist/Debug/Cygwin-Windows/project7_windows.exe] Error 1 

化妝[1]:* [.build設置]錯誤2 化妝:* [.build-IMPL]錯誤2

回答

0

class Node { 
    virtual ~Node(); 
}; 

聲明(但不定義)爲節點虛析構函數(該templateness此處無關緊要)。你要麼需要

  1. 刪除這條線(因而與編譯器提供的默認實現會)或
  2. 提供的定義,以及:

class Node { 
    virtual ~Node() { /* put your dtor logic here */ } 
}; 

不要affraid刪除聲明uless你打算從節點繼承,你不能把太多邏輯節點的析構函數:它不知道太多關於它的模板類型。除非你打算刪除指向下一個節點的指針(Node::link),這可能是一個危險的提議

+0

這對我有用!非常感激! – user1334960 2012-04-16 00:00:04

0

當你的編譯器準確地指出,你宣稱爲~Node(),但從未定義它。您需要提供Node::~Node()的實施。

+0

它現在就編譯!謝謝!如果我沒有實現,我認爲會使用默認的解構器。那是錯的嗎? – user1334960 2012-04-15 23:09:40

+1

如果您未在類中聲明析構函數,則會提供缺省析構函數。既然你宣佈它,你必須提供實施。一條建議。看起來你對C++來說很新穎(通過你上面發佈的評論)。嘗試編寫一些無w/o模板的代碼。解析模板代碼的編譯錯誤並不總是很容易,特別是如果你剛開始學習C++。 – Jagannath 2012-04-15 23:51:57