大家好!我正在制作我自己的链表模板,供练习和将来使用;然而,我的一个功能遇到了问题:
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
};
如果有人能帮我定义一个返回Node指针的函数,我将不胜感激!
谢谢!
由于Node
是在另一个类中声明的,所以在实现中引用它时记得包含类名吗?
LinkedList::Node *LinkedList::FindNode(int x) { ... }
在类声明中,不需要前缀,因为声明在类内部,因此Node
是隐式可用的。