2012-10-08 46 views
0

我一直在为我的某个函数获取编译器错误。C++:关于链表的实现

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; 
}; 
+2

哪里代码你在哪里,你创建一个LinkedList对象? – imreal

+1

81线在哪里? –

+0

线81'code'template ListNode * LinkedList的 ::发现(INT POS) –

回答

2
  1. 为什么当标准库提供了一个创建链表? std::list是一个双链表。
  2. 你能在find()定义改写ListNode*typename LinkedList<T>::ListNode*
  3. 您必须选择是否希望用户能够操纵ListNode,(在这种情况下,你应该把它声明为public),或者如果它是一部分的实现(在这种情况下,您可能想要创建某种类型的迭代器)。

我还是得到了同样的错误

当时的find()定义位于LinkedList类的声明之上,如问题提出?如果是这种情况,你应该换掉它们。

+0

我还是得到了同样的错误 –

1

一两件事,我看到的是,ListNode在LinkedList的定义,因此有资格这样:

template <class T> 
typename LinkedList<T>::ListNode* LinkedList<T>::find(int pos) { 
    ... 
} 
+0

不过它不会工作怪异 –

+0

这里的构造函数和析构函数和复制'code'Template LinkedList的 ::链表()//默认构造 { \t大小= 0; \t head = NULL; } 模板 链表 ::链表(链表和其他)//复制构造 { \t对(INT I = 1; I //拆解 链表 ::〜链表() { \t而(的isEmpty()!) \t \t删除(1); }'code' –

+0

也许把它作为你的问题的一部分?你可以编辑它,而其他人阅读起来会更容易。 –