المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : using Container template class. new to containers help please



C++ Programming
03-29-2009, 08:50 AM
i have started turning this program for a class into a container template class program and i'm having trouble with one part.

Stack *Z = new Stack;

the line above is where i'm having trouble. this line is in my main program and i need to set it up for template but i don't know how to do it for this line i can do templates for everywhere else but because of the * i'm not sure how to handle this one. please any help would be great! thank you!

below is the rest of the code:


#include

using namespace std;

struct Box {
int data;
struct Box * next;
};
template < typename T >
class Stack {

public:
Stack();
~Stack();
bool Pop(int &);
void Push(int);
bool Empty();
private:
struct Box * Start;
};
template
Stack ::Stack() { Start=NULL; }
template
Stack ::~Stack() {

struct Box *p1=Start, *p2;

while (p1!=NULL) {
p2=p1->next;
delete p1;
p1=p2;
}
}
template
bool Stack ::Pop(int &x) {

struct Box *p1;
if (Start==NULL) return false;

x=Start->data;
p1=Start->next;
delete Start;
Start=p1;
return true;
}
template
void Stack ::Push(int x) {

struct Box *p1 = new struct Box;
p1->data = x;
p1->next = Start;
Start=p1;
return;
}
template
bool Stack ::Empty() { if (Start==NULL) return true; else return false; }


int main() {
template
Stack X;
int x;

for (int i=0; i