2014-09-24 103 views
0

大家好!我正在製作我自己的鏈接列表模板,以供練習和未來使用;然而,我遇到了我的一個功能問題:函數返回節點指針

Node * LinkedList :: FindNode(int x); //是爲了遍歷列表並返回一個指向包含x的指針作爲其數據。

當試圖在我的實現文件中聲明它時,我不斷收到Node的消息未定義和不兼容錯誤。

這裏是我的頭文件:

#pragma once 
using namespace std; 

class LinkedList 
{ 
private: 
    struct Node 
    { 
     int data; 
     Node* next = NULL; 
     Node* prev = NULL; 
    }; 

    //may need to typedef struct Node Node; in some compilers 

    Node* head; //points to first node 
    Node* tail; //points to last node 
    int nodeCount; //counts how many nodes in the list 

public: 
    LinkedList(); //constructor 
    ~LinkedList(); //destructor 
    void AddToFront(int x); //adds node to the beginning of list 
    void AddToEnd(int x); //adds node to the end of the list 
    void AddSorted(int x); //adds node in a sorted order specified by user 
    void RemoveFromFront(); //remove node from front of list; removes head 
    void RemoveFromEnd(); //remove node from end of list; removes tail 
    void RemoveSorted(int x); //searches for a node with data == x and removes it from list 
    bool IsInList(int x); //returns true if node with (data == x) exists in list 
    Node* FindNode(int x); //returns pointer to node with (data == x) if it exists in list 
    void PrintNodes(); //traverses through all nodes and prints their data 
}; 

如果有人能幫助我定義返回節點指針的函數,我將不勝感激!

謝謝!

+1

如果一個公共函數應該返回一個'Node *',爲什麼Node是私人的? – 2014-09-24 08:28:29

+1

您確定將自己的鏈接列表模板設置爲「供將來使用」是個不錯的主意嗎?你爲什麼不使用'std :: list'? – TNA 2014-09-24 08:29:38

回答

3

由於Node在另一個類中聲明,您是否記得在實現中引用它時包含類名?

LinkedList::Node *LinkedList::FindNode(int x) { ... } 

在類的聲明則不需要前綴,因爲該聲明是在類中,因此Node隱含可用。

+0

這正是我所期待的。非常感謝你! – 2014-09-24 08:28:51