1
votes

I have been learning C++ for a couple of weeks now and started making my game where I pumped into this problem with std::stack.

I have a class that have a board and a stack

#include <string>
#include <stack>

array<string, 100> board;
stack<array<string, 100>> history;

each time I create my board or make changes I push it to the stack simply by history.push(board). I have made an undo function that looks like this:

array<string, 100> Board::undoMove(stack<array<string, 100>> &history){
    array<string, 100> arr;
    if(!history.empty()){
        history.pop();
        arr = history.top();
    }
    else{
        arr =  array<string, 100>();
    }
    return arr;
}

when calling it:

array<string,100> tempState = b.undoMove(history);
if(!tempState.empty()){
    board = tempState;
    cout << "\nUndoing your latest move.. "<< endl;
}
else{
    cout << "No previous move available" << endl;
}

Apparently, this function works only if I make an undo move after the first change to the board. After a couple of changes, my program stops after a pause. I'm not really good with debugging so I couldn't figure out what is wrong and where. I'm making this game so It helps me understand how stacks work but I don't think this is going anywhere! I think there is something wrong with how I think the stack should work in this case. Also, some people suggested that the fact the stack is a stack of std::array creates the problem when I'm pushing to it but that doesn't sound right, so I'm very confused about how I should look at this problem or think of it and I'm looking for any suggestions I could get.

1
I made tempState to get the result from undoMove function, which if my understanding is ok should be the previous board that we popped out. So if we have something in tempState, meaning if we popped something out of the history that would be the previous move and tempState will not be empty by then. This is basically what i mean by this and how i think it will work - kaMOON
but undoMove always returns a array<string, 100>. That array will never be empty. Your arr = array<string, 100>(); in the else part of the function gives you a non-empty array of 100 default-constructed (empty) strings. - Kevin
oh !!!! wait. so what you mean I should be returning null and comparing to NULL right !!!! I actually came from python so I thought this way array<string, 100>(); I will be initializing an empty array and forgot it will already be full, silly me. thank you for pointing this out - kaMOON
or rather the (pointer) and not the array itself since it can not be null. - kaMOON
@Kevin this, however, does not apply to the std::stack right? I can still check if it's empty that way right? or because it's a stack of arrays this is not allowed? - kaMOON

1 Answers

3
votes
history.pop();

This removes the element at the top of the stack. As in: it goes away. It ceases to be. It goes to meet its maker. It joins the choir invisible. It pines for the fjords. It is now an ex-element. Afterwards:

arr = history.top();

This returns the current element on the top of the stack. This does not return the ex-element. It's ancient history.

So, in the end: if at the beginning of this adventure the stack had only one element, first it gets removed, then an attempt is made to retrieve the element at the top of a completely empty stack. Which, of course, doesn't exist, and the whole house of cards comes tumbling down.

You did these two operations out of order. First you need to retrieve the element at the top of the stack; and only then, after the element is safely tucked away in your own variable, then you get to pop the stack.