0

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!

2
  • Thank you for your responses. I changed LinkedList<typename T> to just LinkedList<T>. However, I'm still receiving all of the same errors. I also changed the pointer declaration as well. Thank you for that catch. Commented Jan 3, 2013 at 19:30
  • Alright, so when I copy pasted all the include code inside the original, it worked perfectly. I think the issue was a problem with namespaces or with the compiler trying to compile linkedlistimplementation on its own without the inclusion of linkedlist.h. However I am still looking for a way to separate functionality from data. Is there any reasonable way of doing this with templates? Commented Jan 3, 2013 at 19:45

4 Answers 4

2

should be:

template <typename T>
LinkedList<T>::LinkedList()
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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;

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.