944,117 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2433
  • C++ RSS
Nov 7th, 2005
0

compile error

Expand Post »
The assignment I am working on is to create a doubly linked list. This is what I have thus far. However when I compile I get 2 errors: LNK2001 unresolved external symbol and LNK1120 unresolved externals. Was hoping someone could look over my code and help me with these errors.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #ifndef H_doublyLinkedList
  3. #define H_doublyLinkedList
  4.  
  5. #include <iostream>
  6. #include <cassert>
  7.  
  8. using namespace std;
  9.  
  10. template <class Type>
  11. struct nodeType
  12. {
  13. Type info;
  14. nodeType<Type> *next;
  15. nodeType<Type> *back;
  16. };
  17.  
  18. template <class Type>
  19. class doublyLinkedList
  20. {
  21. friend ostream& operator<<(ostream&,
  22. const doublyLinkedList<Type>&);
  23.  
  24. public:
  25. const doublyLinkedList<Type>& operator=
  26. (const doublyLinkedList<Type> &);
  27. void initializeList();
  28. bool isEmptyList();
  29. void destroy();
  30. void reversePrint();
  31. int length();
  32. Type front();
  33. Type back();
  34. bool search(const Type& searchItem);
  35. void insertNode(const Type& insertItem);
  36. void deleteNode(const Type& deleteItem);
  37. doublyLinkedList();
  38. doublyLinkedList(const doublyLinkedList<Type>& otherList);
  39. ~doublyLinkedList();
  40.  
  41. protected:
  42. int count;
  43. nodeType<Type> *first;
  44. nodeType<Type> *last;
  45.  
  46. private:
  47. void copyList(const doublyLinkedList<Type>& otherList);
  48. };
  49.  
  50.  
  51. template<class Type>
  52. doublyLinkedList<Type>::doublyLinkedList()
  53. {
  54. first= NULL;
  55. last = NULL;
  56. count = 0;
  57. }
  58.  
  59. template<class Type>
  60. bool doublyLinkedList<Type>::isEmptyList()
  61. {
  62. return(first == NULL);
  63. }
  64.  
  65. template<class Type>
  66. void doublyLinkedList<Type>::destroy()
  67. {
  68. nodeType<Type> *temp;
  69.  
  70. while(first != NULL)
  71. {
  72. temp = first;
  73. first = first->next;
  74. delete temp;
  75. }
  76.  
  77. last = NULL;
  78. count = 0;
  79. }
  80.  
  81. template<class Type>
  82. void doublyLinkedList<Type>::initializeList()
  83. {
  84. destroy();
  85. }
  86.  
  87. template<class Type>
  88. int doublyLinkedList<Type>::length()
  89. {
  90. return count;
  91. }
  92.  
  93. template<class Type>
  94. ostream& operator<<(ostream& osObject,
  95. const doublyLinkedList<Type>& list)
  96. {
  97. nodeType<Type> *current;
  98.  
  99. current = list.first;
  100.  
  101. while(current != NULL)
  102. {
  103. cout<<current->info<<" ";
  104. current = current->next;
  105. }
  106.  
  107. return osObject;
  108. }
  109.  
  110. template<class Type>
  111. void doublyLinkedList<Type>::reversePrint()
  112. {
  113. nodeType<Type> *current;
  114.  
  115. current = last;
  116.  
  117. while(current != NULL)
  118. {
  119. cout<<current->info<<" ";
  120. current = current->back;
  121. }
  122. }
  123.  
  124. template<class Type>
  125. bool doublyLinkedList<Type>::search(const Type& searchItem)
  126. {
  127. bool found;
  128. nodeType<Type> *current;
  129.  
  130. found = false;
  131. current = first;
  132.  
  133. while(current != NULL && !found)
  134. if(current->info >= searchItem)
  135. found = true;
  136. else
  137. current = current->next;
  138.  
  139. if(found)
  140. found = (current->info == searchItem);
  141.  
  142. return found;
  143. }
  144.  
  145. template<class Type>
  146. Type doublyLinkedList<Type>::front()
  147. {
  148. assert(first != NULL);
  149.  
  150. return first->info;
  151. }
  152.  
  153. template<class Type>
  154. Type doublyLinkedList<Type>::back()
  155. {
  156. assert(last != NULL);
  157.  
  158. return last->info;
  159. }
  160.  
  161. template<class Type>
  162. void doublyLinkedList<Type>::insertNode(const Type& insertItem)
  163. {
  164. nodeType<Type> *current;
  165. nodeType<Type> *trailCurrent;
  166. nodeType<Type> *newNode;
  167. bool found;
  168.  
  169. newNode = new nodeType<Type>;
  170. assert(newNode != NULL);
  171.  
  172. newNode->info = insertItem;
  173. newNode->next = NULL;
  174. newNode->back = NULL;
  175.  
  176. if(first == NULL)
  177. {
  178. first = newNode;
  179. last = newNode;
  180. count++;
  181. }
  182. else
  183. {
  184. found = false;
  185. current = first;
  186.  
  187. while(current != NULL && !found)
  188. if(current->info >= insertItem)
  189. found = true;
  190. else
  191. {
  192. trailCurrent = current;
  193. current = current->next;
  194. }
  195.  
  196. if(current == first)
  197. {
  198. first->back = newNode;
  199. newNode->next = first;
  200. first = newNode;
  201. count++;
  202. }
  203. else
  204. {
  205. if(current != NULL)
  206. {
  207. trailCurrent->next = newNode;
  208. newNode->back = trailCurrent;
  209. newNode->next = current;
  210. current->back = newNode;
  211. }
  212. else
  213. {
  214. trailCurrent->next = newNode;
  215. newNode->back = trailCurrent;
  216. last = newNode;
  217. }
  218. count++;
  219. }
  220. }
  221. }
  222.  
  223. template<class Type>
  224. void doublyLinkedList<Type>::deleteNode(const Type& deleteItem)
  225. {
  226. nodeType<Type> *current;
  227. nodeType<Type> *trailCurrent;
  228.  
  229. bool found;
  230.  
  231. if(first == NULL)
  232. cerr<<"Cannot delete from an empty list"<<endl;
  233. else
  234. if(first->info == deleteItem)
  235. {
  236. current = first;
  237. first = first->next;
  238.  
  239. if(first != NULL)
  240. first->back = NULL;
  241. else
  242. last = NULL;
  243.  
  244. count--;
  245. delete current;
  246. }
  247. else
  248. {
  249. found = false;
  250. current = first;
  251.  
  252. while(current != NULL && !found)
  253. if(current->info >= deleteItem)
  254. found = true;
  255. else
  256. current = current->next;
  257.  
  258. if(current == NULL)
  259. cout<<"The item to be deleted is not in the list"
  260. <<endl;
  261. else
  262. if(current->info == deleteItem)
  263. {
  264. trailCurrent = current->back;
  265. trailCurrent->next = current->next;
  266.  
  267. if(current->next != NULL)
  268. current->next->back = trailCurrent;
  269.  
  270. if(current == last)
  271. last = trailCurrent;
  272.  
  273. count--;
  274. delete current;
  275. }
  276. else
  277. cout<<"The item to be deleted is not in list."
  278. <<endl;
  279. }
  280. }
  281.  
  282. template<class Type>
  283. void doublyLinkedList<Type>::copyList(const doublyLinkedList<Type>& otherList)
  284. {
  285. nodeType<Type> *newNode;
  286. nodeType<Type> *current;
  287.  
  288. if(first != NULL)
  289. destroy();
  290.  
  291. if(otherList.first == NULL)
  292. {
  293. first = NULL;
  294. last = NULL;
  295. count = 0;
  296. }
  297. else
  298. {
  299. current = otherList.first;
  300.  
  301. count = otherList.count;
  302.  
  303.  
  304. first = new nodeType<Type>;
  305.  
  306. assert(first != NULL);
  307.  
  308. first->info = current->info;
  309. first->next = NULL;
  310.  
  311. last = first;
  312.  
  313. current = current->next;
  314.  
  315. while(current != NULL)
  316. {
  317. newNode = new nodeType<Type>;
  318.  
  319. assert(newNode!= NULL);
  320.  
  321. newNode->info = current->info;
  322. newNode->next = NULL;
  323.  
  324. last->next = newNode;
  325. last = newNode;
  326.  
  327. current = current->next;
  328. }
  329. }
  330. }
  331.  
  332. template<class Type>
  333. doublyLinkedList<Type>::doublyLinkedList(const doublyLinkedList<Type>& otherList)
  334. {
  335. first = NULL;
  336.  
  337. copyList(otherList);
  338. }
  339.  
  340. template<class Type>
  341. const doublyLinkedList<Type>& doublyLinkedList<Type>::operator=
  342. (const doublyLinkedList<Type> & otherList)
  343. {
  344. if(this != &otherList)
  345. copyList(otherList);
  346.  
  347. return *this;
  348. }
  349.  
  350. template<class Type>
  351. doublyLinkedList<Type>::~doublyLinkedList()
  352. {
  353. destroy();
  354. }
  355.  
  356. #endif
  357.  
  358.  
  359.  
  360. #include <iostream>
  361. #include "doublyLinkedList.h"
  362.  
  363. using namespace std;
  364.  
  365. int main()
  366. {
  367. doublyLinkedList<int> list1, list2;
  368. int num;
  369.  
  370. cout << "Enter integers ending with -999"
  371. << endl;
  372. cin >> num;
  373.  
  374. while(num != -999)
  375. {
  376. list1.insertNode(num);
  377. cin>>num;
  378. }
  379.  
  380. cout << endl;
  381.  
  382. cout << "List 1: " << list1 << endl;
  383.  
  384. list2 = list1;
  385.  
  386. cout << "List 2: " << list2 << endl;
  387.  
  388. cout << "Enter the number to be deleted: ";
  389. cin >> num;
  390. cout << endl;
  391.  
  392. list2.deleteNode(num);
  393.  
  394. cout << "After deleting the node, "
  395. << "List 2: " << endl << list2
  396. << endl;
  397.  
  398. return 0;
  399. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
btech is offline Offline
38 posts
since Oct 2005
Nov 8th, 2005
0

Re: compile error

what function(s) did the compiler say was missing? Look at your code and see if you coded them. If they are in a library somewhare did you include the library?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 8th, 2005
0

Re: compile error

Quote originally posted by Ancient Dragon ...
what function(s) did the compiler say was missing? Look at your code and see if you coded them. If they are in a library somewhare did you include the library?
The compiler did not specify that a function was missing. If I had to guess I would say the error is coming from the copyList function. It looks fine to me but once I added it the errors began.
Reputation Points: 10
Solved Threads: 0
Light Poster
btech is offline Offline
38 posts
since Oct 2005
Nov 8th, 2005
0

Re: compile error

read those error messages again. The compiler (really the linker) is saying that one or more functions are missing. And it tells you the name of them.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 8th, 2005
0

Re: compile error

Well I ran the code, and it compiled and linked just fine. Since you have pasted codes for two files, doublyLinkedList.h and main.cpp in the same code window, I only had to seperate them and compile. Otherthan that I didnt do any changes. THe program ran fine, I entered some integers upto -999 and two lists appeared on the screen. However I got a runtime error in the
C++ Syntax (Toggle Plain Text)
  1. void doublyLinkedList<Type>::deleteNode(const Type& deleteItem)
function. Apparently you are accessing an invalid pointer. If you have coded all this in a single file, ( which I dont think you are, but just in case ), break it up and see. If you get a linker error even then, my guess is you have not included a necessary library, but without the missing function name I cant say specifically what.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 8th, 2005
0

Re: compile error

Quote originally posted by Ancient Dragon ...
read those error messages again. The compiler (really the linker) is saying that one or more functions are missing. And it tells you the name of them.
Here is the error I am getting. As I said I do not see an error about a missing function. But honestly I do not understand how to read the error messages.

C++ Syntax (Toggle Plain Text)
  1. doublyLinkedList error LNK2001: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class doublyLinkedList<int> const &)" (??6@$$FYAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$doublyLinkedList@H@@@Z)
Reputation Points: 10
Solved Threads: 0
Light Poster
btech is offline Offline
38 posts
since Oct 2005
Nov 8th, 2005
0

Re: compile error

Your program compiles and links without error using VC++ 6.0 compiler, just like WolfPack said it would. I tried the same thing with Visual Studio 2003 and got the same error that you did, but unfortunately I havn't been able to figure out the problem either.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 8th, 2005
0

Re: compile error

Quote originally posted by Ancient Dragon ...
Your program compiles and links without error using VC++ 6.0 compiler, just like WolfPack said it would. I tried the same thing with Visual Studio 2003 and got the same error that you did, but unfortunately I havn't been able to figure out the problem either.
I ran the program through my compiler again commenting out lines to see what is causing the error. Turns out the program compiles if I comment out the cout << lines The weird thing is the .h (header file) was supplied to me with the exception of the last four functions. I am really confused now. Could I have written one of the last four functions wrong and that is causing errors in the << operator overload?

The functions I wrote are "copyList", constructor with parameters, = operator overload, and the destructor. Could you verify that those functions are correct? As the error might be originating from one of those functions.
Reputation Points: 10
Solved Threads: 0
Light Poster
btech is offline Offline
38 posts
since Oct 2005
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 8th, 2005
0

Re: compile error

Quote originally posted by Dave Sinkula ...
http://www.parashift.com/c++-faq-lit...html#faq-35.16
Great :mrgreen: coding the friend function at the time it was declared solved the problem.
C++ Syntax (Toggle Plain Text)
  1. template <class Type>
  2. class doublyLinkedList
  3. {
  4. friend ostream& operator<<(ostream& os,
  5. const doublyLinkedList<Type>& list)
  6. {
  7. nodeType<Type> *current;
  8.  
  9. current = list.first;
  10.  
  11. while(current != NULL)
  12. {
  13. os<<current->info<<" ";
  14. current = current->next;
  15. }
  16. return os;
  17. }
  18.  
  19. // rest of class here
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Default values for STL container function arguments?
Next Thread in C++ Forum Timeline: Can anybody tell what's wrong with this code?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC