2017-07-08 115 views
0

我一直在搜索文檔並檢查我的類給出的示例代碼,但無法弄清楚這裏有什麼錯誤。我試圖建立一個鏈表結構,並需要指向節點的指針,但似乎無法正確初始化它們。節點指向對方,它的首要/最後/當前指針的總體類給我帶來了問題。C++指向另一個類中的不同類的指針

當我嘗試製作節點類的指針時,以下代碼給了我一堆錯誤。我在第19,20和21行中獲得了C2238「意外令牌」;'''以及C2143'缺少';'在'*'「和C4430」缺少類型說明符...「之前,第19,20和21行(linkedList的保護部分)再次出現。

template <class Type> 
class linkedList 
{ 
public: 
    //constructors 
    linkedList(); 

    //functions 
    void insertLast(Type data);    //creates a new node at the end of the list with num as its info 
    void print();       //steps through the list printing info at each node 
    int length();       //returns the number of nodes in the list 
    void divideMid(linkedList sublist);  //divides the list in half, storing a pointer to the second half in the private linkedList pointer named sublist 

    //deconstuctors 
    ~linkedList(); 
protected: 
    node *current;       //temporary pointer 
    node *first;       //pointer to first node in linked list 
    node *last;        //pointer to last node in linked list 
    bool firstCreated;      //keeps track of whether a first node has been assigned 
private: 
}; 

template <class Type> 
struct node 
{ 
    Type info; 
    node<Type> *next; 
}; 

如下更改受保護的部分也會導致C2238和C4430錯誤,但會將C2143更改爲「缺失」;「前 '<'」

node<Type> *current;       //temporary pointer 
    node<Type> *first;        //pointer to first node in linked list 
    node<Type> *last;        //pointer to last node in linked list 
+0

你知道如何編寫非模板鏈表嗎? –

回答

1

您需要爲node預先聲明linkedList前:

template <class Type> 
struct node; 

參見工作版本here請。