Pass a Stack by Reference

I have a std::stack that I'd like to pass to a function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <stack>
using namespace std;
bool mess_with_stack(const stack<string>* s);

int main{}
{
  bool test;
  stack<string> s;
  s.push("something");
  test = mess_with_stack(&s);
}

bool mess_with_stack(const stack<string>* s)
{
   if (s.empty())
      return true;
}


The compiler doesn't like the s.empty() call. Anybody see what I am doing wrong?

Thanks.
What you need is an & not a *

bool mess_with_stack(const stack<string>& s)

You did say pass by reference (const stack<string>&) rather than pointer (const stack<string>*)
You need to dereference the pointer to get what it points to. You can do so manually using the '*' operator, or by using the '->' operator.

1
2
if((*s).empty())
    return true;


or

1
2
if(s->empty())
    return true;


Or use a reference instead of a pointer.
That was it. Thanks!
Topic archived. No new replies allowed.