我试图模板化几个类(LinkedListNode和LinkedList),这样
template <class T>
class LinkedListNode{
public:
T data;
LinkedListNode *next;
LinkedListNode();
};
Run Code Online (Sandbox Code Playgroud)
在我的LinkedList类中,我有私有变量:
private:
LinkedListNode *head;
//iterator for traversing the list
LinkedListNode *current;
Run Code Online (Sandbox Code Playgroud)
};
编译时,我遇到了奇怪的错误:
./LinkedList.h:38:3:错误:未知类型名称'LinkedListNode'; 你是说'LinkedList'吗?LinkedListNode*head; ^ ~~~~~~~~~~~~~ LinkedList ./LinkedList.h:13:7:注意:'LinkedList'在这里声明了LinkedList {^ ./LinkedList.h:40:3:错误:未知类型name'LinkedListNode'; 你是说'LinkedList'吗?LinkedListNode*current; ^ ~~~~~~~~~~~~~ LinkedList ./LinkedList.h:13:7:注意:'LinkedList'在这里声明类LinkedList {^ LinkedList.cpp:7:1:错误:'LinkedListNode'是不是类,命名空间或枚举LinkedListNode :: LinkedListNode(){^./ LinkedList.h:5:7:注意:'LinkedListNode'在这里声明类LinkedListNode {^
如果我的LinkedListNode也被声明,为什么我会收到这些错误?
我试图使用<<运算符来调用链表上的插入方法,例如:
a<<b
takes data b and inserts it into LinkedList a
Run Code Online (Sandbox Code Playgroud)
这是我在.h和.cpp文件中的实现:
class LinkedList{
public:
LinkedList();
LinkedList(const LinkedList &l); //copy constructor
~LinkedList(); //destructor
//read the list for items
bool reset();
bool next();
int get();
//insert an item in the list
bool insert();
bool insert(int &data);
//delete an item in the list
bool remove();
//Should take data b and add it to the linked list a
void operator<< (int data);
;
//advances the internal iterator 1 node if not null
void …Run Code Online (Sandbox Code Playgroud) c++ ×2