I am getting these errors in my program :
mingw32-g++.exe -c listAgainToday.cpp -o listAgainToday.o
listAgainToday.cpp:8:34: error: 'list' is not a template
listAgainToday.cpp:25:28: error: 'T' does not name a type
listAgainToday.cpp:25:31: error: ISO C++ forbids declaration of 'val' with no type [-fpermissive]
listAgainToday.cpp:28:27: error: 'T' does not name a type
listAgainToday.cpp:28:30: error: ISO C++ forbids declaration of 'val' with no type [-fpermissive]
listAgainToday.cpp:31:5: error: 'T' does not name a type
listAgainToday.cpp:34:5: error: 'T' does not name a type
listAgainToday.cpp:52:7: error: 'T' does not name a type
listAgainToday.cpp:56:27: error: 'list' is not a template
listAgainToday.cpp:56:40: error: 'std::ostream& list::operator<<(std::ostream&, const list&)' must take exactly one argument
listAgainToday.cpp:83:10: error: expected initializer before '<' token
listAgainToday.cpp:90:11: error: expected initializer before '<' token
listAgainToday.cpp:97:10: error: expected initializer before '<' token
listAgainToday.cpp:107:14: error: expected initializer before '<' token
listAgainToday.cpp:112:10: error: expected initializer before '<' token
listAgainToday.cpp:117:10: error: expected initializer before '<' token
listAgainToday.cpp:131:10: error: expected initializer before '<' token
listAgainToday.cpp:145:7: error: expected initializer before '<' token
listAgainToday.cpp:150:7: error: expected initializer before '<' token
listAgainToday.cpp:155:10: error: expected initializer before '<' token
listAgainToday.cpp:167:10: error: expected initializer before '<' token
listAgainToday.cpp:179:1: error: 'list' is not a template
listAgainToday.cpp:179:15: error: explicit qualification in declaration of 'list list()'
listAgainToday.cpp: In function 'list list()':
listAgainToday.cpp:180:15: error: there are no arguments to 'createEmpty' that depend on a template parameter, so a declaration of 'createEmpty' must be available [-fpermissive]
listAgainToday.cpp:180:15: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
listAgainToday.cpp: At global scope:
listAgainToday.cpp:184:1: error: 'list<T>' does not name a type
listAgainToday.cpp:191:1: error: 'list<T>' does not name a type
listAgainToday.cpp:200:1: error: 'list<T>' does not name a type
listAgainToday.cpp:212:1: error: 'list<T>' does not name a type
listAgainToday.cpp:217:13: error: expected initializer before '::' token
listAgainToday.cpp:231:13: error: expected initializer before '::' token
listAgainToday.cpp: In function 'int main()':
listAgainToday.cpp:249:12: error: expected ';' before 'list1'
listAgainToday.cpp:257:3: error: 'list1' was not declared in this scope
listAgainToday.cpp:262:21: error: 'list1' was not declared in this scope
listAgainToday.cpp:264:2: error: 'list2' was not declared in this scope
listAgainToday.cpp: In instantiation of 'list list() [with T = int]':
listAgainToday.cpp:249:24: required from here
listAgainToday.cpp:180:3: error: 'createEmpty' was not declared in this scope
Process terminated with status 1 (0 minute(s), 0 second(s))
35
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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
#include <iostream>
using namespace std;
class list {
template <typename T>
friend ostream& operator<<(ostream&,
const list<T>&);
//Overload the stream insertion operator
public:
list(); // constructor
list(const list &l); // copy constructor
list &operator=(const list &l); // assignment operator
list &operator<<(const list &l);
~list(); // destructor
// Returns number of elements in the list
unsigned size();
// Returns true if the list is empty, false otherwise.
bool isEmpty() const;
// Inserts element to front of list
void insertFront(const T &val);
// Inserts element to the end of list
void insertBack(const T &val);
// Returns the values of the front element in the list
T front();
// Returns the value of the back element of the list.
T back();
// Deletes the front element of the list and returns its value
void removeFront();
// Deletes the back element of the list and returns its value
void removeBack();
// Prints each element of the list in order
void printForward();
// Prints each element of the list in reverse order
void printReverse();
private:
struct node {
node *next;
node *prev;
T value;
};
template <typename T>
ostream& operator<<(ostream& osObject,
const list<T>& list)
{
node<T> *current;
current = list.first;
while(current != NULL)
{
osObject<< current->info <<" ";
current = current->next;
}
return osObject;
}
node *first; // The pointer to the first node
node *last; // The pointer to the last node
unsigned length; // holds number of elements in the list
// Initializes empty list
void createEmpty();
// Removes all of the elements in the list
void removeAll();
// Makes copy of all of the elements in the list
void copyAll(const list &l);
};
template <typename T>
void list<T>::createEmpty() {
first = NULL;
last = NULL;
length = 0;
}
template <typename T>
void list<T>::removeAll() {
while (!isEmpty()) {
removeFront();
}
}
template <typename T>
void list<T>::copyAll(const list &l) {
node *iterator = l.first;
while (iterator) {
T obj = iterator->value;
insertBack(obj);
iterator = iterator->next;
}
}
template <typename T>
unsigned list<T>::size() {
return length;
}
template <typename T>
bool list<T>::isEmpty() const{
return (first == NULL && last == NULL);
}
template <typename T>
void list<T>::insertFront(const T &val) {
length++;
node *newNode = new node;
new (&(newNode->value)) T(val);
newNode->next = first;
newNode->prev = NULL;
if (isEmpty()) first = last = newNode;
else {
first->prev = newNode;
first = newNode;
}
}
template <typename T>
void list<T>::insertBack(const T &val) {
length++;
node *newNode = new node;
new (&(newNode->value)) T(val);
newNode->next = NULL;
newNode->prev = last;
if (isEmpty()) first = last = newNode;
else {
last->next = newNode;
last = newNode;
}
}
template <typename T>
T list<T>::front() {
return first->value;
}
template <typename T>
T list<T>::back() {
return last->value;
}
template <typename T>
void list<T>::removeFront() {
if (isEmpty()) return;
length--;
node *removedNode = first;
first = first->next;
if (first) first->prev = NULL;
else first = last = NULL;
delete removedNode;
return;
}
template <typename T>
void list<T>::removeBack() {
if (isEmpty()) return;
length--;
node *removedNode = last;
last = last->prev;
if (last) last->next = NULL;
else first = last = NULL;
delete removedNode;
return;
}
template <typename T>
list<T>::list() {
createEmpty();
}
template <typename T>
list<T>::list(const list &l) {
createEmpty();
copyAll(l);
return;
}
template <typename T>
list<T>& list<T>::operator= (const list &l)
{
if (this != &l) {
removeAll();
copyAll(l);
}
return *this;
}
template <typename T>
list<T>& list<T>::operator<< (const list &1)
{
node<T> *current;
current = list.first;
while(current != NULL)
{
osObject << current->info <<" ";
current = current->link;
}
return osObject;
}
template <typename T>
list<T>::~list() {
removeAll();
}
template <typename T>
void list<T>::printForward() {
if (isEmpty()) {
std::cout << "List is empty\n";
return;
}
node *head = first;
while (head) {
std::cout << head->value << " ";
head = head->next;
}
std::cout << "\n";
}
template <typename T>
void list<T>::printReverse() {
if (isEmpty()) {
std::cout << "List is empty\n";
return;
}
node *tail = last;
while (tail) {
std::cout << tail->value << " ";
tail = tail->prev;
}
std::cout << "\n";
}
#include <iostream>
using namespace std;
int main()
{
list<int> list1, list2;
int num;
cout<<"Enter integers ending with -999"<<endl;
cin>> num;
while(num != -999)
{
list1.insertFront(num);
cin >> num;
}
cout<<endl;
cout<<"List 1: " <<list1 << endl;
list2 = list1; //test the assignment operator;
cout <<"List 2: " << list2 <<endl;
cout<<"Enter the number to be deleted: ";
cin>> num;
cout<< endl;
list2.removeFront();
cout<<"After deleting the node, "
<<"List 2: "<<endl <<list2
<<endl;
return 0;
}
|