473,883 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linked list template class doesn't compile

7 New Member
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
7 5783
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
QiongZ
7 New Member
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
9,208 Recognized Expert Moderator Expert
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
QiongZ
7 New Member
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<NODET YPE>' 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
9,208 Recognized Expert Moderator Expert
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
QiongZ
7 New Member
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
wysiswyg
1 New Member
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(11 4) : error C2065: 'NODET
YPE' : undeclared identifier
c:\program files\microsoft visual studio 8\vc\list1.h(12 0) : 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>::getN ewNode'
definition
'ListNode *List1<T>::getN ewNode(T)'
existing declarations
'ListNode<NODET YPE> *List1<T>::getN ewNode(T &)'
Apr 10 '10 #8

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

Similar topics

5
2343
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set up the class link and tel_list. I set up a class person with strings for name, town, and number. I just don't know how to set up the classes w/ their methods (constructors and operations). I'm stuck on the assignment operator and the add and...
57
4322
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
1
3338
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one to use as an index to the freearray cells). Here is the exact problem specifications: Create an ordered list template class named OLType to implement an ordered list with operations of insert, remove, print, empty, full, size. The storage...
4
3219
by: bejiz | last post by:
Hello, I have written a short program for practising linked lists. But there is surely something wrong for when I compile there is a unhandled exception and it does not print if I try to add more than one element to the list. Is this a problem with the function of the internal class Node ? Thanks for any help. Here is the code: #include<iostream>
13
2109
by: B. Williams | last post by:
I have written some code to accept input and place this input at the beginning or end of a list, but I need assistance including a class so that it will allow input of a phone number that is properly formatted. I'll post my code below. Thanks in advance. This is the code for the linked list. It tests using int's and string's. #ifndef LIST_H #define LIST_H
4
5120
by: Beow | last post by:
Hello, I'm still very new to C++ and have encountered some trouble in understanding how exactly the copy constructor for a stack class (given as an example in the book I've been learning from) using a linked list works. The interface for the classes: template<class T> class Node {
1
1333
by: vijit | last post by:
can anyone help me in this linked list please am bit confused 1. get_data: This function finds the data stored in the specified row and column of a 2 dimensional linked list. If data retrieval was successful return true otherwise return false. The prototype is given below: template <class T> bool get_data(int row, int col, T& data); template <class T>
6
3276
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to read my program I appriciate it. the program suppose to print a message on the screen #include <iostream> #include <string>
11
2563
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
0
9942
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10417
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9575
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7972
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7130
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5992
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.