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?