Possible duplicate:
Why can templates be implemented only in the header file?
What is an undefined link / unresolved external character error and how to fix it?
Again, this is homework, and my instructor gave us a lot of feedback, but I'm still lost for this compilation.
When I put the main function in the implementation file, the program compiles and works fine. However, when I put the main function in main.cpp, the compiler complains:
unresolved external symbol "public: __thiscall doublyLinkedList<int>::doublyLinkedList<int>(void)" ( ??0?$doublyLinkedList@H @@ QAE@XZ ) referenced in function
Header file
#ifndef H_doublyLinkedList
Implementation File:
#include <iostream> #include <cassert> #include "doublyLinkedList.h" using namespace std; template <class Type> doublyLinkedList<Type>::doublyLinkedList() { first= NULL; last = NULL; count = 0; } template <class Type> bool doublyLinkedList<Type>::isEmptyList() const { return (first == NULL); } template <class Type> void doublyLinkedList<Type>::destroy() { nodeType<Type> *temp; //pointer to delete the node while (first != NULL) { temp = first; first = first->next; delete temp; } last = NULL; count = 0; } template <class Type> void doublyLinkedList<Type>::initializeList() { destroy(); } template <class Type> int doublyLinkedList<Type>::length() const { return count; } template <class Type> void doublyLinkedList<Type>::print() const { nodeType<Type> *current; //pointer to traverse the list current = first; //set current to point to the first node while (current != NULL) { cout << current->info << " "; //output info current = current->next; }//end while }//end print template <class Type> void doublyLinkedList<Type>::reversePrint() const { nodeType<Type> *current; //pointer to traverse //the list current = last; //set current to point to the //last node while (current != NULL) { cout << current->info << " "; current = current->back; }//end while }//end reversePrint template <class Type> bool doublyLinkedList<Type>:: search(const Type& searchItem) const { bool found = false; nodeType<Type> *current; //pointer to traverse the list current = first; while (current != NULL && !found) if (current->info >= searchItem) found = true; else current = current->next; if (found) found = (current->info == searchItem); //test for //equality return found; }//end search template <class Type> Type doublyLinkedList<Type>::front() const { assert(first != NULL); return first->info; } template <class Type> Type doublyLinkedList<Type>::back() const { assert(last != NULL); return last->info; } template <class Type> void doublyLinkedList<Type>::insert(const Type& insertItem) { nodeType<Type> *current; //pointer to traverse the list nodeType<Type> *trailCurrent; //pointer just before current nodeType<Type> *newNode; //pointer to create a node bool found; newNode = new nodeType<Type>; //create the node newNode->info = insertItem; //store the new item in the node newNode->next = NULL; newNode->back = NULL; if(first == NULL) //if the list is empty, newNode is //the only node { first = newNode; last = newNode; count++; } else { found = false; current = first; while (current != NULL && !found) //search the list if (current->info >= insertItem) found = true; else { trailCurrent = current; current = current->next; } if (current == first) //insert newNode before first { first->back = newNode; newNode->next = first; first = newNode; count++; } else { //insert newNode between trailCurrent and current if (current != NULL) { trailCurrent->next = newNode; newNode->back = trailCurrent; newNode->next = current; current->back = newNode; } else { trailCurrent->next = newNode; newNode->back = trailCurrent; last = newNode; } count++; }//end else }//end else }//end insert template <class Type> void doublyLinkedList<Type>::deleteNode(const Type& deleteItem) { nodeType<Type> *current; //pointer to traverse the list nodeType<Type> *trailCurrent; //pointer just before current bool found; if (first == NULL) cout << "Cannot delete from an empty list." << endl; else if (first->info == deleteItem) //node to be deleted is //the first node { current = first; first = first->next; if (first != NULL) first->back = NULL; else last = NULL; count--; delete current; } else { found = false; current = first; while (current != NULL && !found) //search the list if (current->info >= deleteItem) found = true; else current = current->next; if (current == NULL) cout << "The item to be deleted is not in " << "the list." << endl; else if (current->info == deleteItem) //check for //equality { trailCurrent = current->back; trailCurrent->next = current->next; if (current->next != NULL) current->next->back = trailCurrent; if (current == last) last = trailCurrent; count--; delete current; } else cout << "The item to be deleted is not in list." << endl; }//end else }//end deleteNode template <class Type> void doublyLinkedList<Type>::copyList(const doublyLinkedList<Type>& otherList) { //cout << "The definition of this function is left as an exercise." << endl; //cout << "See Programming Execrise 9." << endl; template <class Type> doublyLinkedList<Type>::doublyLinkedList(const doublyLinkedList<Type>& otherList) { // cout << "The definition of the copy constructor is left as an exercise." << endl; // cout << "See Programming Execrise 9." << endl; } template <class Type> const doublyLinkedList<Type>& doublyLinkedList<Type>::operator= (const doublyLinkedList<Type> &) // cout << "Overloading the assignment operator is left as an exercise." << endl; // cout << "See Programming Execrise 9." << endl; } template <class Type> doublyLinkedList<Type>::~doublyLinkedList() { //cout << "Definition of the destructor is left as an exercise." << endl; //cout << "See Programming Execrise 9." << endl; }
The main function:
//Program to test various operations on a doubly linked list
I am looking for guidance. I know that the answer is really simple, and I just don't see it. I was thinking about using the extern keyword, but I'm not sure how to use it. As I said in the tags, this is homework, so I'm not looking for a quick solution that I want to extract from my mistakes. I really appreciate this site and all the participants.
All the code that I posted here was available for free to the publisher of the book, I did not use my original code, except for main.cpp
Craig source share