0

I am trying to implement a Linked List by a template class and a struct for defining nodes. I expected that it would compile but it's not going to. The error that Visual Studio gives to me is C2955 "use of class template requires template argument list" and checking on Microsoft documentation my code seems to not match to any reported case. The error is reported two times on line 15 of file LinkedList.cpp where the add method start. Do not look at pop function as it is not completely implemented.

LinkedList.h source code:

#pragma once

template <typename T>

struct TNode {
    T info;
    TNode* next;
    TNode* prev;
};

template <typename T>

class LinkedList {
private:
    TNode* head, * tail;
    int size = 0;
public:
    LinkedList();
    void add(T info);
    T pop();
    T peekTail();
    T peekHead();
    void print();
    void rprint();
    uint64_t length();
    bool empty();
    void debug();
};

LinkedList.cpp source code:

#include <cstdlib>
#include <iostream>
#include "LinkedList.h"
using namespace std;

template <typename T>

LinkedList<T>::LinkedList() {
    head = NULL;
    tail = NULL;
}

template <typename T>

void LinkedList<T>::add(T info) {
    TNode* temp = new TNode;
    temp->info = info;
    temp->next = NULL;
    if (head == NULL) {
        head = temp;
        tail = temp;
    }
    else {
        tail->next = temp;
        temp->prev = tail;
        tail = tail->next;
    }
    size++;
}

template <typename T>

T LinkedList<T>::pop() {
    if (size != 0) {
        T info = tail->info;

        size--;
        return info;
    }
}

template <typename T>

T LinkedList<T>::peekTail() {
    return tail->info;
}

template <typename T>

T LinkedList<T>::peekHead() {
    return head->info;
}

template <typename T>

void LinkedList<T>::print() {
    cout << "Elements of the Linked List: ";
    TNode* temp = head;
    while (temp != NULL) {
        cout << temp->info << " ";
        temp = temp->next;
    }
}

template <typename T>

void LinkedList<T>::rprint() {
    cout << "Elements of the Linked List (Reverse): ";
    TNode* temp = tail;
    while (temp != NULL) {
        cout << temp->info << " ";
        temp = temp->prev;
    }
}

template <typename T>

uint64_t LinkedList<T>::length() {
    return size;
}

template <typename T>

bool LinkedList<T>::empty() {
    if (size == 0)
        return true;
    else
        return false;
}

template <typename T>

void LinkedList<T>::debug() {
    cout << length() << endl;
    print();
    cout << endl;
    rprint();
}

How I do solve that problem?

  • For cohesiveness in template entity readability, consider removing the empty lines between the template id (`template-name < parameter-list >`) and the class/function declaration that follows. – dfrib Apr 15 '20 at 16:21
  • 1
    Does this answer your question? [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Richard Critten Apr 15 '20 at 16:23
  • @RichardCritten, that will eventually be a problem, but not related to the error OP is getting. – ChrisMM Apr 15 '20 at 16:40
  • `TNode* temp = new TNode;` -> `TNode* temp = new TNode;` – Adrian Mole Apr 15 '20 at 16:42

1 Answers1

0

You have TNode* head, * tail;, but TNode is a template class, so you must give it a type. In your case, you probably want TNode<T> *head, *tail;, so that the node contains the same as the linked list. Anywhere in your LinkedList where you use TNode would also need to specify the template parameter.

You're going to end up with other problems though. See here as for why.

ChrisMM
  • 8,448
  • 13
  • 29
  • 48