0
votes

Here is the piece of code I am having trouble with:

if (stack_flag == true) {
    bool hangar = false;
    while (hangar == false) {
        unsigned int index = s_move.top();
        unsigned int edge = map[index].get_which_edge();
        char current = map[index].get_char();
        s_move.pop();

A stack container is used if my program takes command line input that says to do so; that's what the stack flag is. This is the first couple lines of my loop. The stack is initialized with one value. The problem I'm having is that I need to get the value of the top element on the stack and I also need to pop that element off the stack right after, but the top() function returns a reference to the top value. When I pop the value off the reference is no longer useful. Is there another way to get the value from the stack while popping it off right after? My error is: s_move was not declared in this scope.

1
s_move was not declared in this scope means what it says: You don't have a stack, or anything else, named s_move that's accessible to this code. This could be a simple typo, or it could mean you're trying to access a local variable from a completely different function, or it could mean you've forgotten to #include something, or… But whatever it is, everything else is irrelevant until you fix that. - abarnert
s_move is declared in a broader scope than the loop and I also have all my includes. there are no typos either. The rest of my code also uses s_move without errors - user2817389
Well, the problem clearly involves code outside what you've shown us, and if you don't show that code, nobody can debug it for you. - abarnert

1 Answers

1
votes

This will work fine:

unsigned int index = s_move.top();

Because index is an l-value. A copy is made using the reference returned by top().
Once you have a copy popping the top will have no affect on index.

The other issue:

My error is: s_move was not declared in this scope.

Is because the code does not know about the object s_move.
Maybe you forget to include the appropriate header file.