I have been attempting to make a generic linked list class for practicing my C++ using templates. However, upon compilation I receive errors and I am clueless on how to solve them. I have spent over 2 hrs attempting to debug one error and have gotten absolutely nowhere. I have also consulted MSDN and google and gotten nowhere either. I'm afraid that I am rather inexperienced when it comes to templates. I have listed the relevant code below. I would greatly appreciate it if someone could assist me.
LinkedList.h:
#ifndef LINKED_LIST
#define LINKED_LIST
namespace D_DATA_STRUCT {
template <typename T>
class LinkedList {
private:
class LinkedListNode {
public:
T data;
LinkedListNode* next;
LinkedListNode(T data) {
this->data = data;
this->next = 0;
}
};
LinkedListNode* head;
LinkedListNode* tail;
public:
LinkedList();
~LinkedList();
void insert_at_head(T data);
void insert_at_tail(T data);
void insert(T data);
T remove_from_head();
T remove();
};
}
#include "LinkedListImplementation.h"
#endif
LinkedListImplementation.h:
namespace D_DATA_STRUCT {
template <typename T>
LinkedList<typename T>::LinkedList() {
this->head = 0;
this->tail = 0;
}
template<typename T>
LinkedList<typename T>::~LinkedList() {
LinkedListNode* prev, next;
for(prev = this->head; prev != 0; prev = prev->next) {
next = prev->next;
delete prev;
prev = next;
}
}
template<typename T>
void LinkedList<typename T>::insert_at_head(T data) {
LinkedListNode* temp = this->head;
this->head = new LinkedListNode(data);
this->head->next = temp;
if(temp == 0) {
this->tail = this->head;
}
return;
}
template<typename T>
void LinkedList<typename T>::insert_at_tail(T data) {
LinkedListNode* temp = new LinkedListNode(data);
if(this->head == 0) {
this->head = temp;
} else {
this->tail->next = temp;
}
this->tail = temp;
return;
}
template<typename T>
void LinkedList<typename T>::insert(T data) {
this->insert_at_head(data);
return;
}
template<typename T>
T LinkedList<typename T>::remove_from_head() {
if(this->head == 0) {
return 0;
}
LinkedListNode* temp = this->head;
T data = temp->data;
this->head = this->head->next;
delete temp;
if(this->head == 0) {
this->tail = 0;
}
return data;
}
template<typename T>
T LinkedList<typename T>::remove() {
return this->remove_from_head();
}
}
The errors I get are:
linkedlistimplementation.h(4): error C2143: syntax error : missing ';' before '<' linkedlistimplementation.h(4): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int linkedlistimplementation.h(4): error C2988: unrecognizable template declaration/definition linkedlistimplementation.h(4): error C2059: syntax error : '<' linkedlistimplementation.h(4): error C2039: 'LinkedList' : is not a member of '`global namespace'' linkedlistimplementation.h(10): error C2588: '::~LinkedList' : illegal global destructor linkedlistimplementation.h(10): fatal error C1903: unable to recover from previous error(s); stopping compilation
Even though there are syntax errors for the ";" the program compiled but failed linking when I erroneously made linkedlistimplementation linkedlist.cpp. Therefore I think there is some problem with my template syntax. I have gone through many docs and tried many things but quite frankly I'm lost and have no clue what's going on. I've tried compiling with typename inside LinkedList:: and without it.
I'm also using Microsoft Visual C++ 2010 Express compiler.
Thank you!