472,362 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,362 software developers and data experts.

linked list template class doesn't compile

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
7 5598
weaknessforcats
9,208 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
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
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

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

Similar topics

5
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...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
1
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...
4
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...
13
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...
4
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)...
1
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...
6
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...
11
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...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.