C++ template linked list help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2009
Posts: 5
Reputation: liemonline is an unknown quantity at this point 
Solved Threads: 0
liemonline liemonline is offline Offline
Newbie Poster

C++ template linked list help

 
0
  #1
16 Days Ago
Hi,

I'm a beginner to C++. I'm writing a linked list template but I get this error:


1>Tester.obj : error LNK2019: unresolved external symbol "public: __thiscall List<int>::~List<int>(void)" (??1?$List@H@@QAE@XZ) referenced in function _main
1>Tester.obj : error LNK2019: unresolved external symbol "public: __thiscall List<int>::List<int>(void)" (??0?$List@H@@QAE@XZ) referenced in function _main


I'm using Visual Studio C++ 2008. I have spent hours trying to solve the problem but fail. I really need your help. Thanks in advance.

The code is quite short and is attached. Thanks.
Attached Files
File Type: zip A8.zip (512.4 KB, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 5
Reputation: liemonline is an unknown quantity at this point 
Solved Threads: 0
liemonline liemonline is offline Offline
Newbie Poster
 
0
  #2
16 Days Ago
Here are all the code

/////////// main.cpp ///////////////
  1. #include "List.h"
  2.  
  3. #include <string>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main( )
  8. {
  9. List<int> myIntList;
  10. cout << "Inserting 1 in the list...\n";
  11. myIntList.insertFirst(1);
  12. myIntList.printList();
  13. cout << endl;
  14. system("Pause");
  15. return 0;
  16. }

////// Link.h/////
  1. #ifndef LIST_H
  2. #define LIST_H
  3.  
  4. template <class T>
  5. class Node
  6. {
  7. public:
  8. Node(){}
  9. Node(T theData, Node<T>* theLink) : data(theData), link(theLink){}
  10. Node<T>* getLink( ) const { return link; }
  11. const T getData( ) const { return data; }
  12. void setData(const T& theData) { data = theData; }
  13. void setLink(Node<T>* pointer) { link = pointer; }
  14. private:
  15. T data;
  16. Node<T> *link;
  17. };
  18.  
  19. template <class T>
  20. class List
  21. {
  22. public:
  23. List( );
  24. //Default constructor.
  25. //Initializes the list to an empty list.
  26.  
  27. bool isEmpty();
  28. //Checks whether the list is empty.
  29.  
  30. void insertFirst(const T& newData);
  31. //Inserts a new node at the beginning of the list
  32. //param T& - new data
  33.  
  34. void printList();
  35. //Print out the list
  36.  
  37. void insertLast(const T& newData);
  38. //Inserts a new node at the end of the list.
  39.  
  40. int length( );
  41. //Returns the length of the list.
  42.  
  43. T front( );
  44. //Returns the information contained in the first node.
  45.  
  46. T back( );
  47. //Retruns the information contained in the last node.
  48.  
  49. bool search(const T item);
  50. //Searches the list for a given item.
  51. //This is a sequential search.
  52.  
  53. void deleteNode(const T& deleteItem);
  54. //Delete a node from the list with a given info.
  55.  
  56. void copyList(List<T>& otherList);
  57. //Make an identical copy of the list
  58.  
  59. void destroyList( );
  60. //Deallocates the memory occupied by each node.
  61.  
  62. void initializeList( );
  63. //Initializes the list to an empty state.
  64. //Note that the default constructor has already initialized
  65. // the list when the list object was declared.
  66.  
  67. ~List( );
  68. //Destructor destroys the list and returns
  69. // all the memory to the freestore when
  70. // the class goes out of scope.
  71.  
  72. private:
  73. Node<T> *first;
  74. //pointer to the first node of the list
  75. Node<T> *last;
  76. //pointer to the last node of the list
  77. int count;
  78. //stores the number of elements in the list
  79. };
  80.  
  81. #endif


/// Link.cpp///
  1.  
  2. #include "List.h"
  3.  
  4. #include<iostream>
  5. using namespace std;
  6.  
  7. template <class T>
  8. List<T>::List()
  9. {
  10. first = NULL;
  11. last = NULL;
  12. count = 0;
  13. }
  14.  
  15. template <class T>
  16. bool List<T>::isEmpty()
  17. {
  18. return (first == NULL);
  19. }
  20.  
  21. template <class T>
  22. void List<T>::insertFirst(const T& newData){
  23. first = new Node(newData, first);
  24. ++count;
  25. }
  26.  
  27. template <class T>
  28. void List<T>::printList(){
  29. Node<T> *tempt;
  30. tempt = first;
  31. while(tempt != NULL){
  32. cout << tempt->getData() << " ";
  33. tempt = tempt->getLink();
  34. }
  35. }
  36.  
  37. template <class T>
  38. void List<T>::insertLast(const T& newData)
  39. {
  40. Node<T> *newNode; //pointer to create the new node
  41. newNode = new Node; //create the new node
  42.  
  43. newNode->setData(newData); //store new data in the node
  44. newNode->setLink(NULL); //insert new node at the end pointing to NULL
  45.  
  46. if (first == NULL) //if the list is empty, newNode is
  47. // both the first and last node
  48. {
  49. first = newNode;
  50. last = newNode;
  51. ++count;
  52. }
  53. else //the list is not empty, insert
  54. // newNode after the last node
  55. {
  56. last->setLink(newNode); //insert newNode after last
  57. last = newNode; //make last point to the actual last node
  58. ++count;
  59. }
  60. }
  61.  
  62. template <class T>
  63. int List<T>::length( )
  64. {
  65. return count;
  66. }
  67.  
  68. template <class T>
  69. T List<T>::front( )
  70. {
  71. return first->getData(); //return the info of the
  72. // first node
  73. }
  74.  
  75. template <class T>
  76. T List<T>::back( )
  77. {
  78. return last->getData(); //return the info of the
  79. // last node
  80. }
  81.  
  82. template <class T>
  83. bool List<T>::search(const T itemToSearch)
  84. {
  85. Node<T> *current; //pointer to traverse the list
  86. bool found;
  87. current = first; //set current to point to the
  88. // first node in the list
  89. found = false;
  90.  
  91. while(current != NULL && !found) //search the list
  92. if (current->getData() == itemToSearch) //the item is found
  93. found = true;
  94. else //otherwise
  95. current = current->getLink(); // make current point
  96. // to the next node
  97. return found;
  98. }
  99.  
  100. template <class T>
  101. void List<T>::deleteNode(const T& deleteItem)
  102. {
  103. Node<T> *current; //pointer to traverse the list
  104. Node<T> *trailCurrent; //pointer just before current
  105. bool found;
  106.  
  107. if(first == NULL) //CASE 1; list is empty
  108. cerr << "Cannot delete from an empty list.\n";
  109. else
  110. { //CASE 2: delete first node
  111. if(first->getData() == deleteItem)
  112. {
  113. current = first;
  114. first = first->getLink();
  115. --count;
  116. if(first == NULL) //list has only one node
  117. last = NULL;
  118. delete current;
  119. }
  120. else //search the list for the node with the given info
  121. {
  122. found = false;
  123. trailCurrent = first; //set trailCurrent to point to
  124. // the first node
  125. current = first->getLink(); //set current to point to the
  126. // second node
  127.  
  128. while(current != NULL && !found)
  129. {
  130. if(current->getData() != deleteItem)
  131. {
  132. trailCurrent = current;
  133. current = current->getLink();
  134. }
  135. else
  136. found = true;
  137. } // end while
  138.  
  139. if(found) //CASE 3; if found, delete the node
  140. {
  141. trailCurrent->setLink(current->getLink());
  142. --count;
  143.  
  144. if(last == current) //node to be deleted was
  145. // the last node
  146. last = trailCurrent; //update the value of last
  147.  
  148. delete current; //delete the node from the list
  149. }
  150. else
  151. cout << "Item to be deleted is not in the list." << endl;
  152. }
  153. }
  154. }
  155.  
  156. template <class T>
  157. void List<T>::copyList(List<T>& otherList)
  158. {
  159. Node<T> *newNode; //pointer to create a node
  160. Node<T> *current; //pointer to traverse the list
  161.  
  162. if(first != NULL) //if the list is non-empty, make it empty
  163. destroyList();
  164.  
  165. if(otherList.first == NULL) //otherList is empty
  166. {
  167. first = NULL;
  168. last = NULL;
  169. count = 0;
  170. }
  171. else
  172. {
  173. current = otherList.first; //current points to the
  174. //list to be copied
  175. count = otherList.count;
  176.  
  177. //copy the first node
  178. first = new Node; //create the node
  179.  
  180. first->setData(current->getData()); //copy the info
  181. first->setLink(NULL); //set the link field of
  182. // the node to NULL
  183. last = first; //make last point to the
  184. // first node
  185. current = current->getLink(); //make current point to
  186. // the next node
  187.  
  188. //copy the remaining list
  189. while(current != NULL)
  190. {
  191. newNode = new Node; //create a node
  192.  
  193. newNode->setData(current->getData()); //copy the info
  194. newNode->setLink(NULL); //set the link of
  195. // newNode to NULL
  196. last->setLink(newNode); //attach newNode after last
  197. last = newNode; //make last point to
  198. // the actual last node
  199. current = current->getLink(); //make current point to
  200. // the next node
  201. }
  202. }
  203. }
  204.  
  205. template <class T>
  206. void List<T>::destroyList( )
  207. {
  208. Node<T> *temp; //pointer to deallocate the
  209. // memory occupied by the node
  210. while(first != NULL) //while there are nodes in the list
  211. {
  212. temp = first; //set temp to the current node
  213. first = first->getLink(); //advance first to the next node
  214. delete temp; //deallocate the memory occupied by temp
  215. }
  216.  
  217. last = NULL; //iniitialize last to NULL
  218. // (first has already been set to NULL
  219. // by the while loop)
  220. count = 0;
  221. }
  222.  
  223. template <class T>
  224. void List<T>::initializeList( )
  225. {
  226. destroyList( ); //if the list has any nodes,
  227. // delete them
  228. }
  229.  
  230. template <class T>
  231. List<T>::~List( )
  232. {
  233. destroyList();
  234. }
Last edited by liemonline; 16 Days Ago at 3:22 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 306
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 33
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #3
16 Days Ago
Maybe I'm missing something, but do you actually create myIntList (instantiate your List object) before you attempt to use it in main()?
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 5
Reputation: liemonline is an unknown quantity at this point 
Solved Threads: 0
liemonline liemonline is offline Offline
Newbie Poster
 
0
  #4
16 Days Ago
oh, I missed that line when I posted the code but it was actually there.
I edited my post to correct it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 306
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 33
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #5
16 Days Ago
Part of it is your header is link.h and you include "list.h" (which even though it's in quotes, it ends up finding some older header called list.h in the search path). There's some stuff with your node class too.
Last edited by jonsca; 16 Days Ago at 3:40 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 5
Reputation: liemonline is an unknown quantity at this point 
Solved Threads: 0
liemonline liemonline is offline Offline
Newbie Poster
 
0
  #6
16 Days Ago
OK, so this is interesting. I have deleted most of the code but I still get the same error:

Tester.obj : error LNK2019: unresolved external symbol "public: __thiscall List<int>::List<int>(void)" (??0?$List@H@@QAE@XZ) referenced in function _main

//// Tester.cpp /////
  1. #include "List.h"
  2.  
  3. #include <string>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main( )
  8. {
  9.  
  10. List<int> myIntList;
  11.  
  12. system("Pause");
  13. return 0;
  14. }



//// List.h /////
  1. #ifndef LIST_H
  2. #define LIST_H
  3.  
  4.  
  5. template <class T>
  6. class List
  7. {
  8. public:
  9. List();
  10. };
  11.  
  12. #endif



/// List.cpp ////
  1. #include "List.h"
  2.  
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. template <class T>
  7. List<T>::List()
  8. {
  9. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 306
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 33
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #7
16 Days Ago
I'd change them all to link rather than risking collision with that other header file (which is probably the non-standard STL list header)
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 5
Reputation: liemonline is an unknown quantity at this point 
Solved Threads: 0
liemonline liemonline is offline Offline
Newbie Poster
 
0
  #8
16 Days Ago
thank you very much. Btw, I have solved the problem. I was silly not including the line in my Tester.cpp file

#include "List.cpp"
Last edited by liemonline; 16 Days Ago at 4:01 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 306
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 33
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #9
16 Days Ago
I was just playing around with that (well, putting your link.cpp into your link.h to see if it was the compiler being finicky about templates). Anyway glad it worked for ya.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC