2014-09-24 102 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