user1946492
user1946492

Reputation: 31

Creating a LinkedList with templates -- Errors

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!

Upvotes: 0

Views: 983

Answers (4)

Nikanos Polykarpou
Nikanos Polykarpou

Reputation: 391

another problem is in this line

LinkedListNode* prev, next;

only prev is a pointer to LinkedListNode. next will be an object and will try to call the default constructor of LinkedListNode which does not exist.

If you want both of them to be pointers it should be changed to

LinkedListNode *prev, *next;

Upvotes: 2

thugthrasher
thugthrasher

Reputation: 29

I don't know if there are any other errors, but the thing that immediately stuck out to me is that in your function definitions, you don't need "typename" twice, you only use it in the line that starts "template."

As an example:
Your constructor is defined

template <typename T>
    LinkedList<typename T>::LinkedList() {
        this->head = 0;
        this->tail = 0;
    }

when it should be

template <typename T>
    LinkedList<T>::LinkedList() {
        this->head = 0;
        this->tail = 0;
    }

Make that change in all your function definitions and you should be okay.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409176

You have your implementation declarations wrong, the class name should not have the typname part. Change the constructor in the implementation to

template<typename T>
LinkedList<T>::LinkedList() {
    // ...
}

Do the same for the other methods as well.

Upvotes: 1

user1252446
user1252446

Reputation:

should be:

template <typename T>
LinkedList<T>::LinkedList()

Upvotes: 2

Related Questions