468,248 Members | 1,534 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,248 developers. It's quick & easy.

Template and Inheritance problem - Please Help

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.
Jul 22 '05 #1
3 1816

Gandu wrote:
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.

Jul 22 '05 #2
Gandu wrote:
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

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;
}


Jul 22 '05 #3
Gandu wrote:
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;
}

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tony Johansson | last post: by
1 post views Thread by Tony Johansson | last post: by
2 posts views Thread by Thomas Kowalski | last post: by
45 posts views Thread by charles.lobo | last post: by
9 posts views Thread by stephen.diverdi | last post: by
32 posts views Thread by Stephen Horne | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.