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