Thread: STL Stack Example

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    STL Stack Example

    Hi. I am working with the following code from a Data Structures text. When I try to instantiate a Stack object (as in Stack<int> s;), I get an error message in Dev-C++, "Instantiated from here," among others. Can anyone see what the problem might be? Thanks, Steve
    Code:
    #include<iostream>
    using namespace std;
    
    template<class T>
    struct Node
    {
        T data;
        Node<T> *next;
    };
    
    template<class T>
    class Stack
    {
        private:
            int count;
            Node <T> top;
        public:
            Stack();
            ~Stack();
            bool pushStack(T dataIn);
            bool popStack(T& dataOut);
            bool stackTop(T& dataOut);
            bool emptyStack();
            bool fullStack();
            int stackCount();
    };
    
    template<class T>
    Stack<T>::Stack()
             :top(0), count(0){}
             
    template<class T>
    bool Stack<T>::pushStack(T dataIn)
    {
        bool success;
        Node<T> *newPtr;
        
        if(!(newPtr = new Node<T>))
            success=false;
        else
        {
            newPtr->data=dataIn;
            newPtr->next=top;
            top=newPtr;
            count++;
            success=true;
        }
        return success;
    }
    
    template<class T>
    bool Stack<T>::popStack(T& dataOut)
    {
        Node<T> *dltPtr;
        bool success;
        
        if(count==0)
            success=false;
        else
        {
            dltPtr=top;
            dataOut=top->data;
            top=top->next;
            count--;
            success=true;
        }
        return success;
    }
    
    int main()
    {
        return 0;
    }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    This:
    Code:
            Node <T> top;
    should be this:
    Code:
            Node <T> *top;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM