-2

My code is as follows:

template <class Elem>
void LList<Elem>::LList(const int sz) { // ERROR
    head = tail = curr = new link; // Create header
    head->next = head;
}

template <class Elem>
void LList<Elem>::clear() { // Remove Elems
    while (head->next != NULL) { // Return to free
        curr = head->next; // (keep header)
        head->next = curr->next;
        delete curr;
    }
    tail = curr = head->next = head; // Reinitialize
}

// Insert Elem at current position
template <class Elem>
void LList<Elem>::insert(const Elem& item) {
    assert(curr != NULL); // Must be pointing to Elem
    curr->next = new link(item, curr->next);
    if (tail->next != head) tail = tail->next;
}

template <class Elem> // Put at tail
void LList<Elem>::append(const Elem& item)
    { tail = tail->next = new link(item, head); }

// Move curr to next position
template <class Elem>
void LList<Elem>::next()
    { curr = curr->next; }

// Move curr to prev position
template <class Elem>
void LList<Elem>::prev() {
    link* temp = curr;
    while (temp->next!=curr) temp=temp->next;
    curr = temp;
}

I use vscode to run this code, but it gives a compiler error for this part:

template<class Elem>
void LList<Elem>::LList(const int sz){ // ERROR
    head = tail = curr = new link; // Create header
    head->next = head;
}

The compiler error is:

LList is not a template

How can I fix the error?

chris
  • 60,560
  • 13
  • 143
  • 205
ALEX
  • 7
  • Maybe it's similar to this : https://stackoverflow.com/q/30367936/10957435 –  Sep 15 '19 at 05:49
  • 1
    I've [edit]ed your question to include the information you posted as an answer. Since it's not an answer and that info is now in your question, I'd suggest deleting the answer. – chris Sep 15 '19 at 06:34
  • That said, the code still feels like it has pieces missing. While it's good to remove things that aren't required to reproduce the error, all the things that are needed and other contextual information should be there. For example, if your class is in its own header and this code is its own file, that's good to mention. In this case, it's possible that you didn't include the header declaring `LList`, and also possible that [Why can templates only be implemented in the header file?](https://stackoverflow.com/q/495021/962089) is very relevant. – chris Sep 15 '19 at 06:39

1 Answers1

0

I'm assuming LList is a class you're creating?

in that case try defining all of that in your class

template<class Elem>
class LList
{
    void LList<Elem>::LList(const int sz)
    {

    }
};

I mean, that function has no name (therefor I assume it's a constructor?)

stylo
  • 496
  • 4
  • 12
  • I show all the code in my next answer, please teach me how to let the code running, thank you – ALEX Sep 15 '19 at 06:24