By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

linked list template class doesn't compile

P: 7
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below:


Expand|Select|Wrap|Line Numbers
  1. // test-linked-list.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include "list1.h"
  7.  
  8. using std::cout;
  9. using std::cin;
  10. using std::endl;
  11.  
  12. void testIntegerList();
  13. void instructions();
  14.  
  15. int _tmain(int argc, _TCHAR* argv[])
  16. {
  17.     testIntegerList();
  18.     return 0;
  19. }
  20.  
  21. void testIntegerList()
  22. {
  23.     cout << "Testing a List of integer values" << endl;
  24.  
  25.     List1<int> integerList;
  26.  
  27.     instructions();
  28.  
  29.     int choice, value;
  30.  
  31.     do {
  32.         cout << "? ";
  33.  
  34.         cin >> choice;
  35.  
  36.         switch (choice) {
  37.  
  38.             case 1:
  39.                 cout << "Enter an integer: ";
  40.                 cin >> value;
  41.                 integerList.insertAtFront(value);
  42.                 integerList.print();
  43.                 break;
  44.  
  45.             case 2:
  46.                 cout << "Enter an integer: ";
  47.                 cin >> value;
  48.                 integerList.insertAtBack(value);
  49.                 integerList.print();
  50.                 break;
  51.  
  52.             case 3:
  53.                 if (integerList.removeFromFront(value))
  54.                     cout << "integer removed from front" << endl;
  55.                 integerList.print();
  56.                 break;
  57.  
  58.             case 4:
  59.                 if (integerList.removeFromBack(value))
  60.                     cout << "integer removed from back" << endl;
  61.  
  62.                 integerList.print();
  63.                 break;
  64.  
  65.         }
  66.     } while (choice != 5);
  67.  
  68.     cout << "End test of integer list" << endl;
  69. }
  70.  
  71. void instructions()
  72. {
  73.     cout << "Enter one of the following:" << endl
  74.         << " 1 to insert at beginning of list" << endl
  75.         << " 2 to insert at the end of list" << endl
  76.         << " 3 to delete from beginning of list" << endl
  77.         << " 4 to delete from end of list" << endl
  78.         << " 5 to end list processing" << endl;
  79. }
  80.  
  81. // list1.h
  82. #ifndef LIST1_H
  83. #define LIST1_H
  84.  
  85. #include "listnd.h"
  86.  
  87.  
  88. template<typename T>
  89. class List1 {
  90. public:
  91.     List1();
  92.     ~List1();
  93.     void insertAtFront(T &);
  94.     void insertAtBack(T &);
  95.     int removeFromFront(T &);
  96.     int removeFromBack(T &);
  97.     int isEmpty();
  98.     void print();
  99. private:
  100.     ListNode<T> *firstPtr;
  101.     ListNode<T> *lastPtr;
  102.     ListNode<T> *getNewNode(T &);
  103. };
  104.  
  105.  
  106. #endif
  107.  
  108.  
  109. // list1.cpp
  110. #include "stdafx.h"
  111. #include "list1.h"
  112. #include <iostream>
  113. #include <assert.h>
  114. using std::cout;
  115. using std::endl;
  116.  
  117. template<typename T>
  118. List1<T>::List1() {firstPtr = lastPtr = 0;}
  119.  
  120. template<typename T>
  121. List1<T>::~List1()
  122. {
  123.     if (!isEmpty()) {
  124.         cout << "Destroying nodes ... " << endl;
  125.  
  126.         ListNode<NODETYPE> *currentPtr = firstPtr, *tempPtr;
  127.         while (currentPtr != 0) {
  128.             tempPtr = currentPtr;
  129.             cout << tempPtr->data << endl;
  130.             currentPtr = currentPtr->nextPtr;
  131.             delete tempPtr;
  132.         }
  133.     }
  134.  
  135.         cout << "All nodes destroyed" << endl << endl;
  136. }
  137.  
  138. template<typename T>
  139. void List1<T>::insertAtFront(T &value)
  140. {
  141.     ListNode<NODETYPE> *newPtr = getNewNode(value);
  142.  
  143.     newPtr->nextPtr = firstPtr;
  144.     firstPtr = newPtr;
  145. }
  146.  
  147. template<typename T>
  148. void List1<T>::insertAtBack(T &value)
  149. {
  150.     ListNode<NODETYPE> *newPtr = getNewNode(value);
  151.  
  152.     lastPtr->nextPtr = newPtr;
  153.     //newPtr->nextPtr = 0;
  154.     lastPtr = newPtr;
  155. }
  156.  
  157. template<typename T>
  158. int List1<T>::removeFromFront(T &value)
  159. {
  160.     if (isEmpty())
  161.         return 0;
  162.     else {
  163.         ListNode<NODETYPE> *tempPtr = firstPtr;
  164.         if (firstPtr == lastPtr)
  165.             firstPtr = lastPtr = 0;
  166.         else
  167.             firstPtr = firstPtr->nextPtr;
  168.  
  169.         value = tempPtr->data;
  170.         delete tempPtr;
  171.         return 1;
  172.     }
  173. }
  174.  
  175. template<typename T>
  176. int List1<T>::removeFromBack(T &value)
  177. {
  178.     if (isEmpty())
  179.         return 0;
  180.     else {
  181.         ListNode<NODETYPE> *tempPtr = lastPtr;
  182.         if (firstPtr == lastPtr)
  183.             firstPtr = lastPtr = 0;
  184.         else {
  185.             ListNode<NODETYPE> *currentPtr = firstPtr;
  186.             while (currentPtr->nextPtr != lastPtr)
  187.                 currentPtr = currentPtr->nextPtr;
  188.  
  189.             lastPtr = currentPtr;
  190.             currentPtr->nextPtr = 0;
  191.         }
  192.  
  193.         value = tempPtr-data;
  194.         delete tempPtr;
  195.         return 1;
  196.     }
  197. }
  198.  
  199. template<typename T>
  200. int List1<T>::isEmpty() {return firstPtr == 0;}
  201.  
  202. template<typename T>
  203. ListNode<NODETYPE> *List1<T>::getNewNode(T value)
  204. {
  205.     ListNode<NODETYPE> *ptr = new ListNode<NODETYPE>(value);
  206.  
  207.     assert(ptr != 0);
  208.     return ptr;
  209. }
  210.  
  211. template<typename T>
  212. void List1<T>::print()
  213. {
  214.     if (isEmpty()) {
  215.         cout << "The list is empty" << endl << endl;
  216.         return;
  217.     }
  218.  
  219.     ListNode<NODETYPE> *currentPtr = firstPtr;
  220.  
  221.     cout << "The list is: ";
  222.  
  223.     while (currentPtr != 0) {
  224.         cout << currentPtr->data << ' ';
  225.         currentPtr = currentPtr->nextPtr;
  226.     }
  227.  
  228.     cout << endl << endl;
  229. }
  230.  
  231. // listnd.h
  232. #ifndef LISTND_H
  233. #define LISTND_H
  234.  
  235. template<typename NODETYPE>
  236. class ListNode {
  237.     friend class List1<T>;
  238.     public: 
  239.         ListNode(NODETYPE &);
  240.         ~ListNode();
  241.         NODETYPE getData();
  242.     private: 
  243.         NODETYPE data;            
  244.         ListNode *nextPtr;
  245. };
  246.  
  247. #endif
  248.  
  249. // listnd.cpp
  250. #include "stdafx.h"
  251. #include "listnd.h"
  252.  
  253. template<typename NODETYPE>
  254. ListNode<NODETYPE>::ListNode(NODETYPE &info)
  255. {
  256.     data = info;
  257.     nextPtr = 0;
  258. }
  259.  
  260. template<typename NODETYPE>
  261. NODETYPE ListNode<NODETYPE>::getData() {return data;}


That's the codes. I get errors like the following:

Expand|Select|Wrap|Line Numbers
  1. 1>Compiling...
  2. 1>list1.cpp
  3. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2059: syntax error : '<'
  4. 1>        e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(14) : see reference to class template instantiation 'ListNode<NODETYPE>' being compiled
  5. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2238: unexpected token(s) preceding ';'
  6. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.h(22) : error C2989: 'List1' : class template has already been declared as a non-class template
  7. 1>        e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : see declaration of 'List1'
  8. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.h(7) : error C3857: 'List1': multiple template parameter lists are not allowed
  9. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.cpp(9) : error C2988: unrecognizable template declaration/definition
  10. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.cpp(9) : error C2059: syntax error : '<'
  11. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.cpp(12) : error C2588: '::~List1' : illegal global destructor
  12. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.cpp(12) : fatal error C1903: unable to recover from previous error(s); stopping compilation
  13. 1>listnd.cpp
  14. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2059: syntax error : '<'
  15. 1>        e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(14) : see reference to class template instantiation 'ListNode<NODETYPE>' being compiled
  16. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2238: unexpected token(s) preceding ';'
  17. 1>test-linked-list.cpp
  18. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2059: syntax error : '<'
  19. 1>        e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(14) : see reference to class template instantiation 'ListNode<NODETYPE>' being compiled
  20. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2238: unexpected token(s) preceding ';'
  21. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.h(22) : error C2989: 'List1' : class template has already been declared as a non-class template
  22. 1>        e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : see declaration of 'List1'
  23. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.h(7) : error C3857: 'List1': multiple template parameter lists are not allowed
  24. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(25) : error C2143: syntax error : missing ';' before '<'
  25. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(25) : error C2143: syntax error : missing ';' before '<'
  26. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(41) : error C2065: 'integerList' : undeclared identifier
  27. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(41) : error C2228: left of '.insertAtFront' must have class/struct/union
  28. 1>        type is ''unknown-type''
  29. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(42) : error C2065: 'integerList' : undeclared identifier
  30. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(42) : error C2228: left of '.print' must have class/struct/union
  31. 1>        type is ''unknown-type''
  32. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(48) : error C2065: 'integerList' : undeclared identifier
  33. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(48) : error C2228: left of '.insertAtBack' must have class/struct/union
  34. 1>        type is ''unknown-type''
  35. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(49) : error C2065: 'integerList' : undeclared identifier
  36. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(49) : error C2228: left of '.print' must have class/struct/union
  37. 1>        type is ''unknown-type''
  38. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(53) : error C2065: 'integerList' : undeclared identifier
  39. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(53) : error C2228: left of '.removeFromFront' must have class/struct/union
  40. 1>        type is ''unknown-type''
  41. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(55) : error C2065: 'integerList' : undeclared identifier
  42. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(55) : error C2228: left of '.print' must have class/struct/union
  43. 1>        type is ''unknown-type''
  44. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(59) : error C2065: 'integerList' : undeclared identifier
  45. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(59) : error C2228: left of '.removeFromBack' must have class/struct/union
  46. 1>        type is ''unknown-type''
  47. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(62) : error C2065: 'integerList' : undeclared identifier
  48. 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(62) : error C2228: left of '.print' must have class/struct/union
  49. 1>        type is ''unknown-type''
  50. 1>Generating Code...
  51. 1>Build log was saved at "file://e:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\test-linked-list\Debug\BuildLog.htm"
  52. 1>test-linked-list - 32 error(s), 0 warning(s)

Thank you for taking time to look at this. I have been looking at this for a couple days now and really need some help here. Thank you!

Qiong
Nov 13 '09 #1
Share this Question
Share on Google+
7 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Which textbook are you using?

There are so mnay errors in this example I can't believe it's in a book.
Nov 13 '09 #2

P: 7
C++ How to program by Deitel and Deitel.

What kind of errors? Can you point out some of them for me? Thank you for your reply.

One thing to note again: if I remove all the templates, the program compiled successfully.

Qiong
Nov 13 '09 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
I found the example. The Deitel book I have is dated 1994. That's before templates were actually working in C++.

Further, you don't write linked lists in C++. Instead you use the C++ Standard Library list template.

This book is so old that there was no C++ Standard Library. It didn't appear until 1998.

In fact, all C++ books prior to September 1998 are pre-ANSI C++ and should be discarded.

Get a newer book.

In ANSI C++ a template is a pattern. When you specialize the template, the compiler makes a copy of it and replaces the typename placeholder with the actual type.

Since the compiler needs to have the template in order ot make a copy, the template is a declaration and therefore all templates are placed in header files.

The specializations are placed in the .cpp files.

All this being said, it appears the example was not copied exactly into your computer. Like a typename of T appears whereas in the example everything is typename NODETYPE.

I would make sure that all the temp[lates are in .h files. The only .cpp file you should have is the Driver.cpp file.


Finally is appears your Visual Studio project is not set up correctly. You have a Windows console program and you really want a Console program without the Windows stuff.

Do this:

1) Create a Win32 application.
2) When trhe wizard appears, DO NOT CLICK FINISH.
3) Instead, click application settings.
4) Select a) empty project and b) console application
5) Now click FINISH.

Post again if you are still stuck.
Nov 13 '09 #4

P: 7
Thank you very much for your reply. I made a few changes you suggested. I put templates all in header files and I am listing them again so that it is easy to see on which lines the errors occurred.

linked-list-driver.cpp
Expand|Select|Wrap|Line Numbers
  1. // linked-list-driver.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. //#include "stdafx.h"
  5. #include <iostream>
  6. #include "list1.h"
  7.  
  8. using std::cout;
  9. using std::cin;
  10. using std::endl;
  11.  
  12. void testIntegerList();
  13. void instructions();
  14.  
  15. main()
  16. {
  17.     testIntegerList();
  18.     return 0;
  19. }
  20.  
  21. void testIntegerList()
  22. {
  23.     cout << "Testing a List of integer values" << endl;
  24.  
  25.     List1<int> integerList;
  26.  
  27.     instructions();
  28.  
  29.     int choice, value;
  30.  
  31.     do {
  32.         cout << "? ";
  33.  
  34.         cin >> choice;
  35.  
  36.         switch (choice) {
  37.  
  38.             case 1:
  39.                 cout << "Enter an integer: ";
  40.                 cin >> value;
  41.                 integerList.insertAtFront(value);
  42.                 integerList.print();
  43.                 break;
  44.  
  45.             case 2:
  46.                 cout << "Enter an integer: ";
  47.                 cin >> value;
  48.                 integerList.insertAtBack(value);
  49.                 integerList.print();
  50.                 break;
  51.  
  52.             case 3:
  53.                 if (integerList.removeFromFront(value))
  54.                     cout << "integer removed from front" << endl;
  55.                 integerList.print();
  56.                 break;
  57.  
  58.             case 4:
  59.                 if (integerList.removeFromBack(value))
  60.                     cout << "integer removed from back" << endl;
  61.  
  62.                 integerList.print();
  63.                 break;
  64.  
  65.         }
  66.     } while (choice != 5);
  67.  
  68.     cout << "End test of integer list" << endl;
  69. }
  70.  
  71. void instructions()
  72. {
  73.     cout << "Enter one of the following:" << endl
  74.         << " 1 to insert at beginning of list" << endl
  75.         << " 2 to insert at the end of list" << endl
  76.         << " 3 to delete from beginning of list" << endl
  77.         << " 4 to delete from end of list" << endl
  78.         << " 5 to end list processing" << endl;
  79. }

list1.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef LIST1_H
  2. #define LIST1_H
  3.  
  4. //#include "stdafx.h"
  5. #include "listnd.h"
  6. #include <iostream>
  7. #include <assert.h>
  8. using std::cout;
  9. using std::endl;
  10.  
  11. template<class T>
  12. class List1 {
  13. public:
  14.     List1();
  15.     ~List1();
  16.     void insertAtFront(T &);
  17.     void insertAtBack(T &);
  18.     int removeFromFront(T &);
  19.     int removeFromBack(T &);
  20.     int isEmpty();
  21.     void print();
  22. private:
  23.     ListNode<T> *firstPtr;
  24.     ListNode<T> *lastPtr;
  25.     ListNode<T> *getNewNode(T &);
  26. };
  27.  
  28. template<typename T>
  29. List1<T>::List1() {firstPtr = lastPtr = 0;}
  30.  
  31. template<typename T>
  32. List1<T>::~List1()
  33. {
  34.     if (!isEmpty()) {
  35.         cout << "Destroying nodes ... " << endl;
  36.  
  37.         ListNode<NODETYPE> *currentPtr = firstPtr, *tempPtr;
  38.         while (currentPtr != 0) {
  39.             tempPtr = currentPtr;
  40.             cout << tempPtr->data << endl;
  41.             currentPtr = currentPtr->nextPtr;
  42.             delete tempPtr;
  43.         }
  44.     }
  45.  
  46.         cout << "All nodes destroyed" << endl << endl;
  47. }
  48.  
  49. template<typename T>
  50. void List1<T>::insertAtFront(T &value)
  51. {
  52.     ListNode<NODETYPE> *newPtr = getNewNode(value);
  53.  
  54.     newPtr->nextPtr = firstPtr;
  55.     firstPtr = newPtr;
  56. }
  57.  
  58. template<typename T>
  59. void List1<T>::insertAtBack(T &value)
  60. {
  61.     ListNode<NODETYPE> *newPtr = getNewNode(value);
  62.  
  63.     lastPtr->nextPtr = newPtr;
  64.     //newPtr->nextPtr = 0;
  65.     lastPtr = newPtr;
  66. }
  67.  
  68. template<typename T>
  69. int List1<T>::removeFromFront(T &value)
  70. {
  71.     if (isEmpty())
  72.         return 0;
  73.     else {
  74.         ListNode<NODETYPE> *tempPtr = firstPtr;
  75.         if (firstPtr == lastPtr)
  76.             firstPtr = lastPtr = 0;
  77.         else
  78.             firstPtr = firstPtr->nextPtr;
  79.  
  80.         value = tempPtr->data;
  81.         delete tempPtr;
  82.         return 1;
  83.     }
  84. }
  85.  
  86. template<typename T>
  87. int List1<T>::removeFromBack(T &value)
  88. {
  89.     if (isEmpty())
  90.         return 0;
  91.     else {
  92.         ListNode<NODETYPE> *tempPtr = lastPtr;
  93.         if (firstPtr == lastPtr)
  94.             firstPtr = lastPtr = 0;
  95.         else {
  96.             ListNode<NODETYPE> *currentPtr = firstPtr;
  97.             while (currentPtr->nextPtr != lastPtr)
  98.                 currentPtr = currentPtr->nextPtr;
  99.  
  100.             lastPtr = currentPtr;
  101.             currentPtr->nextPtr = 0;
  102.         }
  103.  
  104.         value = tempPtr-data;
  105.         delete tempPtr;
  106.         return 1;
  107.     }
  108. }
  109.  
  110. template<typename T>
  111. int List1<T>::isEmpty() {return firstPtr == 0;}
  112.  
  113. template<typename T>
  114. ListNode<NODETYPE> *List1<T>::getNewNode(T value)
  115. {
  116.     ListNode<NODETYPE> *ptr = new ListNode<NODETYPE>(value);
  117.  
  118.     assert(ptr != 0);
  119.     return ptr;
  120. }
  121.  
  122. template<typename T>
  123. void List1<T>::print()
  124. {
  125.     if (isEmpty()) {
  126.         cout << "The list is empty" << endl << endl;
  127.         return;
  128.     }
  129.  
  130.     ListNode<NODETYPE> *currentPtr = firstPtr;
  131.  
  132.     cout << "The list is: ";
  133.  
  134.     while (currentPtr != 0) {
  135.         cout << currentPtr->data << ' ';
  136.         currentPtr = currentPtr->nextPtr;
  137.     }
  138.  
  139.     cout << endl << endl;
  140. }
  141.  
  142. #endif
  143.  

listnd.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef LISTND_H
  2. #define LISTND_H
  3.  
  4. //#include "stdafx.h"
  5.  
  6. template<typename NODETYPE>
  7. class ListNode {
  8.     friend class List1<T>;
  9.     public: 
  10.         ListNode(NODETYPE &);
  11.         ~ListNode();
  12.         NODETYPE getData();
  13.     private: 
  14.         NODETYPE data;            
  15.         ListNode *nextPtr;
  16. };
  17.  
  18. template<typename NODETYPE>
  19. ListNode<NODETYPE>::ListNode(NODETYPE &info)
  20. {
  21.     data = info;
  22.     nextPtr = 0;
  23. }
  24.  
  25. template<typename NODETYPE>
  26. NODETYPE ListNode<NODETYPE>::getData() {return data;}
  27.  
  28. #endif
  29.  

I got fewer errors this time:

listnd.h(8) : error C2059: syntax error : '<'
listnd.h(16) : see reference to class template instantiation 'ListNode<NODETYPE>' being compiled
listnd.h(8) : error C2238: unexpected token(s) preceding ';'
list1.h(26) : error C2989: 'List1' : class template has already been declared as a non-class template
listnd.h(8) : see declaration of 'List1'
list1.h(11) : error C3857: 'List1': multiple template parameter lists are not allowed
list1.h(29) : error C2988: unrecognizable template declaration/definition
list1.h(29) : error C2059: syntax error : '<'
list1.h(32) : error C2588: '::~List1' : illegal global destructor
list1.h(32) : fatal error C1903: unable to recover from previous error(s); stopping compilation

Qiong
Nov 13 '09 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
This cose os your first error:

Expand|Select|Wrap|Line Numbers
  1.  friend class List1<T>; 
I suggest you research bound vs. unbound friends in templates.

The C++ Primer Plus 5th edition has a good explanantion. And there is Google.
Nov 13 '09 #6

P: 7
The problem is solved. Someone suggested me to add

Expand|Select|Wrap|Line Numbers
  1. template< typename T > class List1;
in listnd.h before declaration of ListNode.

And also I did the following two things:

1 took destructor ~ListNode() out of listnd.h
2. make sure all the typename (for class List1 and class ListNode) same


that did the trick.

Thank you very much for your help, weaknessforcats. Hope this post can help someone else in the future.

Qiong
Nov 13 '09 #7

P: 1
Hi Qiong,

Can you give me the correct code, using your program I am still getting the following errors at compile time.

linked-list-driver.cpp
c:\program files\microsoft visual studio 8\vc\list1.h(114) : error C2065: 'NODET
YPE' : undeclared identifier
c:\program files\microsoft visual studio 8\vc\list1.h(120) : error C2244: 'List1
<T>::getNewNode' : unable to match function definition to an existing declaratio
n
c:\program files\microsoft visual studio 8\vc\list1.h(25) : see declarat
ion of 'List1<T>::getNewNode'
definition
'ListNode *List1<T>::getNewNode(T)'
existing declarations
'ListNode<NODETYPE> *List1<T>::getNewNode(T &)'
Apr 10 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.