-1

I searched for similar questions, but could not find one that I could apply to my situation. I have a stack class using an array class as a data member. I test it with push and pop in 'main()' on a stack of size 5 (in this case, data member 'Array m_array'). I use a for loop to fill the stack with postfix incrementation, but instead of pushing 0 into the first element, it pushes 1. My output is:

1
2
3
4

3
2
1
0
-1
Stack is full(empty)

But I want it to be

0
1
2
3...

In test:

int main()
{
    try {
        Stack<int> s(5);
        for (int i = 0; i < 4; i++) {
            s.Push(i);
            cout << s << endl;
        }

        cout << endl;

        for (int i = 0; i < 8; ++i) {
            s.Pop();
            cout << s << endl;
        }
    }

    catch (ArrayException &ex)    //catch object
    {
        cout << ex.GetMessage() << endl;
    }

    catch(...)    //default catch
    {
        cout << "An unhandled exception has occurred" << endl;
    }
    return 0;
}

In stack.cpp:

#ifndef STACK_CPP
#define STACK_CPP
#include "stack.h"

namespace Jules
{
    namespace Containers
    {
        template<typename T>    //default constructor
        Stack<T>::Stack() : m_array(Array<T>()), m_current(0)
        {
        }

        template<typename T>    //destructor
        Stack<T>::~Stack()
        {
        }

        template<typename T>    //copy constructor
        Stack<T>::Stack(const Stack<T> &s) : m_array(s.m_array), m_current(s.m_current)
        {
        }

        template<typename T>    //constructor with input argument
        Stack<T>::Stack(const int i) : m_array(i), m_current(0)
        {
        }

        template<typename T>    //assignment operator
        Stack<T> Stack<T>::operator=(Stack<T> const &source)
        {
            if (this == &source)
                return *this;
            Stack<T>::operator = (source);
            return *this;
        }

        template<typename T>    //push function
        void Stack<T>::Push(const T& element)
        {
            m_array[m_current] = element;
            ++m_current;
        }

        template<typename T>    //pop function
        void Stack<T>::Pop()
        {
            --m_current;
            m_array[m_current];
        }

        template<typename T2>    //send to ostream
        ostream& operator << (ostream& os, const Stack<T2>& t)
        {
            os << t.m_current;
            return os;
        }
    }
}
#endif
tripleee
  • 175,061
  • 34
  • 275
  • 318
kits
  • 609
  • 6
  • 20
  • What does 'operator << (ostream &, const Stack&)' do? – Desu_Never_Lies Feb 20 '15 at 06:12
  • It's the ostream operator so I can cout stack objects. – kits Feb 20 '15 at 06:14
  • "it pushes 1". How do you know? You are not printing what's pushed, you are printing the stack itself, and the code for printing the stack is not available. – n. m. could be an AI Feb 20 '15 at 06:19
  • In `Stack::Pop()`, your first line is `m_array[m_current];`. This does literally nothing (except read out of bounds of the array, but that's another story). Did you mean to return the popped element? I personally would rewrite your push method as: `m_array[m_current++] = element; if(m_current > size) enlarge();`, and your pop method as `if(m_current == 0) throw SomeException(); return m_array[--m_current];` – C0deH4cker Feb 20 '15 at 06:38
  • Also, maybe you intended to implement a queue instead of a stack? When you push 1, 2, 3 to a stack and then pop all values, you will get 3, 2, 1. With a queue, pushing 1, 2, 3 and then popping will give you 1, 2, 3. – C0deH4cker Feb 20 '15 at 06:39
  • You should really show us your code in your stack for printing. Otherwise no one can help you. – Daniel Feb 20 '15 at 06:48
  • My suspicion is that you are printing the value of m_current instead of getting the value from your stack. – Daniel Feb 20 '15 at 06:51
  • I will add the stack.cpp to my post, hopefully it's not too big. – kits Feb 20 '15 at 06:53

1 Answers1

0

The code for your operator << simply prints the size of the stack. So you are not seeing any stack values printed, just the size. You were just unlucky that the size of the stack correlated so closely to the values you were pushing on to it.

What would you expect an operator << do? Maybe it should iterate over the stack contents and print them all out? Maybe it should just say "Stack Size: " before the stack size, then it would be obvious.

The Dark
  • 8,453
  • 1
  • 16
  • 19
  • It's a perfect correlation as far as I can tell, it's pushing and popping in my loop. I don't understand what the issue is here. My original post said nothing about the ostream operator, I don't understand why you brought that up? Thanks – kits Feb 20 '15 at 08:20
  • 1
    You said that the output was incorrect. The output is done using this line `cout << s << endl;`. That line invokes your `operator <<` method. Your `operator <<` method prints the size of the stack. So currently your code pushes a value onto the stack and prints the size of the stack. – The Dark Feb 20 '15 at 16:57