Yoda
Yoda

Reputation: 18068

Error while putting string into stack C++

I have a big problem with "program language" called C++. I wanted to print the stack of strings.

void show(stack<string> stos) {
  while (!stos.empty()) {
    cout << stos.pop() << endl;
  }
}

Upvotes: 0

Views: 97

Answers (2)

jrok
jrok

Reputation: 55395

pop() only removes the top element from the stack and throws it away. It returns void (nothing) and you can't print that with cout, obviously. You need:

void show(stack<string> stos)
{
    while(!stos.empty()) {
        cout << stos.top() << endl;
        stos.pop();
    }
}

Upvotes: 5

Ben Voigt
Ben Voigt

Reputation: 283684

pop doesn't return the value removed. You have to first access top() to get the value, then call pop() to get rid of it.

Upvotes: 0

Related Questions