472,954 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

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 2149

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
3
by: Gandu | last post by:
Could some C++ guru please help me. I have a template based general linked list class that I want to inherit publicly to create a queue class. In the case of non-template inheritance, I can use:...
2
by: Tony Johansson | last post by:
Hello Experts To derive a concrete class from a template class in invalid. Why?? So you should not be able have something like this class Derived : public Base<int, 100> It's valid to derive...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. Im I right if I say the...
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
6
by: chris.kemmerer | last post by:
I am having a problem with templates and I hope someone here can help. I am writing a library that accepts data packets, parses them and saves the information for later use. One member of the...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.