G
Gandu
Could some C++ guru please help me? I have a very odd problem with respect
templates and inheritance. I have templatized List class, from which I am
inheriting to create a Stack class. All works fine till I have to create a
Stack object - all sorts of error messages are generated. I have included
source files below:
The List class:
#include "List.h"
template <class T>
List<T>::List():start(NULL),currpos(NULL){}
template <class T>
List<T>::~List(){
node<T> *nptr = start;
while(nptr != currpos){
start = start->next;
delete nptr;
nptr = start;
}
delete nptr;
}
template <class T>
void List<T>::addNodeAtStart(T &t){
node<T>* temp = NULL;
if(start == NULL){
start = new node<T>();
start->item = t;
currpos = start;
}
if(start != NULL){
temp = new node<T>();
temp->item = t;
temp->next = start;
start = temp;
}
}
template <class T>
void List<T>::addNodeAtEnd(T& t){
node<T>* temp = NULL;
if(start == NULL){
start = new node<T>();
start->item = t;
currpos = start;
}
if(start != NULL){
temp = new node<T>();
temp->item = t;
currpos->next = temp;
currpos = temp;
}
}
template <class T>
void List<T>::deleteFromEnd(){
node<T> *temp1 = start;
node<T> *temp2 = NULL;
while(temp1 != currpos){
temp1 = temp1->next;
if(temp1->next == currpos){
temp2 = temp1;
break;
}
}
if(temp2 != NULL){
delete temp2->next;
currpos = temp2;
}
}
template <class T>
void List<T>::deleteFromStart(){
node<T> *temp = start;
start = start->next;
delete temp;
}
template <class T>
void List<T>::show(){
node<T> *temp = start;
while(temp != currpos){
cout<<"\t"<<temp->item<<endl;
temp = temp->next;
}
}
//End of class
/*
int main(int argc, char **argv){
List<int> iList;
int i0 = 0;
int i1 = 1;
int i2 = 2;
int i3 = 3;
iList.addNodeAtStart(i0);
iList.addNodeAtStart(i1);
iList.addNodeAtStart(i2);
iList.addNodeAtStart(i3);
iList.show();
cout<<"\tDeleting from end ..."<<endl;
iList.deleteFromEnd();
iList.show();
cout<<"\tDeleting from start ..."<<endl;
iList.deleteFromStart();
iList.show();
return 0;
}
*/
The Stack class is:
#include "Stack.h"
template <class T>
Stack<T>::Stack(){List<T>();}
template <class T>
Stack<T>::~Stack(){}
template <class T>
void Stack<T>:
ush(T& t){
List<T>::addNodeAtStart(t);
}
template <class T>
void Stack<T>:
op(){
List<T>::deleteFromStart();
}
template <class T>
void Stack<T>::show(){
List<T>::show();
}
int main(int argc, char **argv){
Stack<int> intStack;
int i0 = 0;
int i1 = 1;
int i2 = 2;
int i3 = 4;
intStack.push(i0);
intStack.push(i1);
intStack.push(i2);
intStack.push(i3);
intStack.show();
intStack.pop();
intStack.show();
return 0;
}
The trouble appears to be in the following line: Stack<int> intStack;
I would greatly appreciate if someone could point out what I did wrong.
templates and inheritance. I have templatized List class, from which I am
inheriting to create a Stack class. All works fine till I have to create a
Stack object - all sorts of error messages are generated. I have included
source files below:
The List class:
#include "List.h"
template <class T>
List<T>::List():start(NULL),currpos(NULL){}
template <class T>
List<T>::~List(){
node<T> *nptr = start;
while(nptr != currpos){
start = start->next;
delete nptr;
nptr = start;
}
delete nptr;
}
template <class T>
void List<T>::addNodeAtStart(T &t){
node<T>* temp = NULL;
if(start == NULL){
start = new node<T>();
start->item = t;
currpos = start;
}
if(start != NULL){
temp = new node<T>();
temp->item = t;
temp->next = start;
start = temp;
}
}
template <class T>
void List<T>::addNodeAtEnd(T& t){
node<T>* temp = NULL;
if(start == NULL){
start = new node<T>();
start->item = t;
currpos = start;
}
if(start != NULL){
temp = new node<T>();
temp->item = t;
currpos->next = temp;
currpos = temp;
}
}
template <class T>
void List<T>::deleteFromEnd(){
node<T> *temp1 = start;
node<T> *temp2 = NULL;
while(temp1 != currpos){
temp1 = temp1->next;
if(temp1->next == currpos){
temp2 = temp1;
break;
}
}
if(temp2 != NULL){
delete temp2->next;
currpos = temp2;
}
}
template <class T>
void List<T>::deleteFromStart(){
node<T> *temp = start;
start = start->next;
delete temp;
}
template <class T>
void List<T>::show(){
node<T> *temp = start;
while(temp != currpos){
cout<<"\t"<<temp->item<<endl;
temp = temp->next;
}
}
//End of class
/*
int main(int argc, char **argv){
List<int> iList;
int i0 = 0;
int i1 = 1;
int i2 = 2;
int i3 = 3;
iList.addNodeAtStart(i0);
iList.addNodeAtStart(i1);
iList.addNodeAtStart(i2);
iList.addNodeAtStart(i3);
iList.show();
cout<<"\tDeleting from end ..."<<endl;
iList.deleteFromEnd();
iList.show();
cout<<"\tDeleting from start ..."<<endl;
iList.deleteFromStart();
iList.show();
return 0;
}
*/
The Stack class is:
#include "Stack.h"
template <class T>
Stack<T>::Stack(){List<T>();}
template <class T>
Stack<T>::~Stack(){}
template <class T>
void Stack<T>:
List<T>::addNodeAtStart(t);
}
template <class T>
void Stack<T>:
List<T>::deleteFromStart();
}
template <class T>
void Stack<T>::show(){
List<T>::show();
}
int main(int argc, char **argv){
Stack<int> intStack;
int i0 = 0;
int i1 = 1;
int i2 = 2;
int i3 = 4;
intStack.push(i0);
intStack.push(i1);
intStack.push(i2);
intStack.push(i3);
intStack.show();
intStack.pop();
intStack.show();
return 0;
}
The trouble appears to be in the following line: Stack<int> intStack;
I would greatly appreciate if someone could point out what I did wrong.