0

I keep on getting a compiler error for one of my functions.

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

But the thing is I have an constructor, destructor, and type conversion. I'm pretty sure that the implementation is wrong

// 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;
};
5
  • 2
    Where is the code where you are you creating a LinkedList object? Commented Oct 8, 2012 at 1:31
  • line 81 is codetemplate <class T> ListNode* LinkedList<T>::find(int pos) Commented Oct 8, 2012 at 1:33
  • You have to put the class declaration before the definition of ListNode* LinkedList<T>::find(int pos) Commented Oct 8, 2012 at 1:35
  • i did that. I just put it that way here so you can see right away what i was having trouble with. Commented Oct 8, 2012 at 1:37
  • I think you will have trouble returning a private nested type, ListNode. Commented Oct 8, 2012 at 1:39

2 Answers 2

2
  1. Why create a linked list when the standard library provides one? std::list is a double-linked list.
  2. Can you rewrite ListNode* to typename LinkedList<T>::ListNode* in the find() definition
  3. You will have to choose whether you want the user to be able to manipulate ListNode, (in which case you should declare it as public), or if it is part of the implementation (in which case you might want to create an iterator of some sort).

I still got the same error

Was the definition of find() located on top of the declaration of the LinkedList class, as presented in the question? If that is the case you should swap them around.

Sign up to request clarification or add additional context in comments.

Comments

1

One thing I see is that ListNode is defined in LinkedList, so has to be qualified that way:

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

2 Comments

here is the constructor and destructor and copy codeTemplate <class T> LinkedList<T>::LinkedList() //Default constructor { size = 0; head = NULL; } template <class T> LinkedList<T>::LinkedList(LinkedList &other) //Copy Constructor { for(int i = 1; i < other.getLength(); i++) insert(i, other.retrieve(i)); } template <class T>//Deconstructor LinkedList<T>::~LinkedList() { while(!isEmpty ()) remove(1); }code
Perhaps post it as part of your question? You can edit it, and it would be much easier for others to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.