Thread: stack

  1. #1
    Registered User
    Join Date
    Dec 2001


    I have a problem that is probably something very simple I'm overlooking. I am trying to build a stack and output it. It is compiling, but gives an error that isn't related to my main before it runs. I'm using the header <stack.h> and here is the way I'm trying to use it.
    #include <iostream.h>
    #include <stack.h>
    void main()
    Stack<int> s;
    for (int i = 0;i<5;i++)

    I'm doing something wrong, and I can't figure out what. If someone could show me an example of how to use the stack.h file, it would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2001
    I guess it should be "stack", not "Stack".

  3. #3
    depending on which compiler you are using and what version of stack header file you are using (STL stack class versus your own stack class), there are several possibilities. If you have an up to date compiler you may need to drop the .h's and use a namespace as suggested my Manish. You also may need to use lower case s in the type indicator when declaring stack s. Then you go to output the value of s, you can't do it all at once like you try. You need to do it one element at a time, just like you do an array. The only difference between a generic stack and an array is that with an array you can add and remove randomly, whereas with a stack the last item you added must be the first item you remove. In the STL stack class I believe the [] operator is overloaded so you can access the elements of the stack just like you would the elements of an array.

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. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM