Template and Inheritance problem - Please Help

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>::push(T& t){
List<T>::addNodeAtStart(t);
}

template <class T>
void Stack<T>::pop(){
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.
 
P

Patrik Stellmann

Gandu said:
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.
Just a pitty that you didn't post any of them...

Could it be that you wrote your template-implementation in a cpp-file?
In that case the compiler can hardly generate code from it when
compiling the line "Stack<int> intStack;". If this is the case you
should either put it at the bottom of your header or write in your
header "#include "stack.inl" and place that code in the inline-file.
 
A

Amith

Gandu said:
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"

its easier for us to debug if u can post the definition of the classes
"List" and "Stack" and also the error messages you are getting.

-Amith
C++ compiler team
Hewlett Packard
 
G

Gianni Mariani

Gandu said:
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:

Because you left out the header files - I had to reconstruct the
contents of the headers. Once I did this, your code ran and the output was:


4
2
1
0
2
1
0

Here is the "reconstructed" code. BTW - don't use NULL, use 0 for nil
pointers. If this is written for some study, then no problems, however
if you think you're doing better than std::slist, think again.


//#include "List.h"

#include <iostream>
using namespace std;

template <class T>
struct node
{
node * next;
T item;
};

template <class T>
struct List
{

node<T> * start;
node<T> * currpos;

List();
~List();
void addNodeAtStart(T &t);
void addNodeAtEnd(T& t);
void deleteFromEnd();
void deleteFromStart();
void show();
};


template <class T>
List<T>::List():start(0),currpos(0){}

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 = 0;

if(start == 0){
start = new node<T>();
start->item = t;
currpos = start;
}

if(start != 0){
temp = new node<T>();
temp->item = t;
temp->next = start;
start = temp;
}
}


template <class T>
void List<T>::addNodeAtEnd(T& t){
node<T>* temp = 0;

if(start == 0){
start = new node<T>();
start->item = t;
currpos = start;
}

if(start != 0){
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 = 0;

while(temp1 != currpos){
temp1 = temp1->next;
if(temp1->next == currpos){
temp2 = temp1;
break;
}
}

if(temp2 != 0){
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>
struct Stack : List<T>
{
Stack();
~Stack();
void push(T& t);
void pop();
void show();
};

template <class T>
Stack<T>::Stack(){List<T>();}

template <class T>
Stack<T>::~Stack(){}

template <class T>
void Stack<T>::push(T& t){
List<T>::addNodeAtStart(t);
}

template <class T>
void Stack<T>::pop(){
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top