Hello, I am attempting to create a stack.h file using templates and a linklist in order to hold the desrired values I "push" into the stack, but unfortunately it does not like the way I am attempting the problem... any help here would be greatly appreciated:
CONTENTS OF DRIVER FILE:
Code:#include <iostream> #include "stack2.h" using namespace std; int main() { stack<int> myStack; myStack.push(7); myStack.push(3); myStack.push(3); myStack.push(1); while (myStack.count()) { cout<<myStack.pop() << endl; } return 0; }
CONTENTS OF STACK.H FILE
Code:#include <iostream.h> template <typename T> struct item { T value; item* ptr; }; template <class T> class stack { public: private: int counter; item* start; public: stack(); int count(); void push(T newtop); T pop(); }; template <typename T> stack<T>::stack(){ counter = 0; start = NULL; } template <typename T> int stack<T>::count(){ return counter; } template <typename T> void stack<T>::push(T newtop){ item* temp = new item; temp->value = newtop; //you may have to fill in other values. temp->ptr = start; start = temp; counter++; } template <typename T> T stack<T>::pop(){ if (counter > 0){ counter--; T popValue = start->value; start = start->ptr; return popValue; } else{ return T(); //or whatever you want your error value to be } }



LinkBack URL
About LinkBacks


