I am new to C++ and am trying to create a doubly linked list using templates, here is the code

doubleLinkedList.h

#ifndef DOUBLELINKEDLIST_H_
#define DOUBLELINKEDLIST_H_

#include<iostream>
#include"nodeType.h"

using namespace std;


template <class Type>
class doubleLinkedList
{
      public:
             void initList();
             bool isEmptyList() const;
             void destroy();
             void print() const;
             void reversePrint() const;
             int length() const;
             
             Type front() const;
             Type back() const;
             
             bool search(const Type& searchItem) const;
             void insert(const Type& insertItem);
             void deleteNode(const Type& deleteItem);
             
             doubleLinkedList();
             
             protected:
                       int count;
                       nodeType<Type> *first;
                       nodeType<Type> *last;
};
             


#endif /* DOUBLELINKEDLIST_H_ */

nodetype.h

#ifndef NODETYPE_H_
#define NODETYPE_H_

#include<iostream>


using namespace std;

template <class Type>
struct nodeType
{
       Type info;
       nodeType<Type> *next;
       nodeType<Type> *back;
};

#endif /* NODETYPE_H_ */

the implementation file

#include<iostream>
#include"doubleLinkedList.h"
#include"nodeType.h"

using namespace std;

template <class Type>
doubleLinkedList<Type>::doubleLinkedList()
{
    first= NULL;
    last = NULL;
    count = 0;
}

template <class Type>
Type doubleLinkedList<Type>::front() const
{
     assert(first != NULL);
     
     return first->info;
}

template <class Type>
Type doubleLinkedList<Type>::back() const
{
     assert(last != NULL);
     
     return last->info;
}

and the driver

#include<iostream>
#include"doubleLinkedList.h"
#include"nodeType.h"

using namespace std;

int main()
{
    doubleLinkedList<int> testOne;
	cout << "working" << endl;
    
    system("pause");
    return 0;
}

Not completely finished, but I keep getting a " [Linker error] undefined reference to `doubleLinkedList<int>::doubleLinkedList()' " using devC++ (required by the course) Anyone know how to fix this?

Recommended Answers

All 3 Replies

In C++ you cannot seperate the template header from its definition. So You can't have
LinkedList.cpp. You need to put the definition in the same file as the header file.

In C++ you cannot seperate the template header from its definition. So You can't have
LinkedList.cpp. You need to put the definition in the same file as the header file.

Thats actually how I originally had it and I got the same error

Well first fix the header and cpp issue. That error message is saying that
the compiler cannot find the constructor's definition.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.