C++:关于链表的实现
C++: Implementation concerning linked list
我的一个函数不断收到编译器错误。
LinkedList.hpp:81: error: `template<class T> class LinkedList' used without template parameters
LinkedList.hpp:81: error: expected constructor, destructor, or type conversion before '*' token
LinkedList.hpp:81: error: expected `;' before '*' token
但问题是我有一个构造函数、析构函数和类型转换。我很确定实现是错误的
// This is the function i keep on getting an error for
template <class T>
ListNode* LinkedList<T>::find(int pos)//Finds the position of an item
{
if(pos < 1)
return NULL; //If pos is less than one then find returns NULL because pos is a illegal value.
else
{
ListNode *temp = head;
for(int i = 1; i < pos; i++)
temp = temp -> next;
return temp;
}
}
//The class
template <class T>
class LinkedList : public ABCList<T> {
private:
//T a [LIST_MAX];
struct ListNode
{
T data; // List item
ListNode *next; //Pointer to next node
};
int size;
ListNode *head;
ListNode *find(int pos);
public:
LinkedList();
LinkedList(LinkedList &other);
~LinkedList();
virtual bool isEmpty () = 0;
virtual int getLength () = 0;
virtual void insert (int pos, T item) = 0;
virtual T remove (int pos) = 0;
virtual T retrieve (int pos) = 0;
};
- 当标准库提供链表时,为什么要创建一个链表?
std::list
是一个双向链表。 - 你能重写
ListNode*
以find()
定义中的typename LinkedList<T>::ListNode*
吗 - 您必须选择是否希望用户能够操作
ListNode
(在这种情况下,您应该将其声明为公共),或者它是否是实现的一部分(在这种情况下,您可能希望创建某种迭代器)。
我仍然收到相同的错误
find()
的定义是否如问题中所述位于LinkedList
类声明之上?如果是这种情况,您应该交换它们。
我看到的一件事是ListNode是在LinkedList中定义的,所以必须以这种方式进行限定:
template <class T>
typename LinkedList<T>::ListNode* LinkedList<T>::find(int pos) {
...
}
相关文章:
- 如果没有malloc,链表实现将失败
- 不了解链表实现,请帮忙.C++
- 使用链表实现堆栈时出错
- 如何编写一个类似于kernellist_head的c++风格的双链表实现
- C++中链表实现的问题
- 链表实现 c++
- 难以理解链表实现(结构部分)?
- 为什么下面的使用链表实现线性队列的代码返回垃圾值然后崩溃
- 如何将自己的链表实现从存储整数更改为存储个人数据
- 在我的C++链表实现中取消引用节点指针,给出意想不到的结果
- C++ 中堆栈的链表实现
- c++ 中 if 语句中的多个条件(通过链表实现堆栈)
- C++ 访问数据的链表实现
- 使用链表实现队列
- 此链表实现中的错误
- 试图用c++编写我自己的链表实现,在点击列表中的3个元素后编写segfault代码
- 通过数组实现BST,通过链表实现堆
- 从后到前显示使用链表实现的队列
- C++链表实现多数据插入
- 反转使用链表实现的堆栈