Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Stack to Function

So I'm playing around with stacks and I've filled one in my main function, but now I want to pass it to my other functions so I can traverse through it. I'm not sure what kind of data type to put into the prototype though so that it accepts it. Suggestions? Here's what I have:

Main.cpp

#include <iostream>
using namespace std;
#include "stack.h"

void displayStack(char &stackRef);

int main()
{
    Stack<char> stack;
    stack.push('a');
    stack.push('b');
    stack.push('c');

    return 0;
};

void displayStack(char starRef)
{
    // Cannot Get here - Errors!
};

It's telling me I have too many arguments and it doesn't match argument list.

like image 920
Howdy_McGee Avatar asked Oct 25 '12 03:10

Howdy_McGee


People also ask

Can we pass stack as reference?

Just use std::stack. You can pass in a reference, maybe that's more efficient.. If you don't want it to change use a reference to const.

What is passing argument to function?

Passing arguments to function is a very important aspect of C++ programming. Arguments refer to values that can be passed to a function. Furthermore, this passing of arguments takes place for the purpose of being used as input information.

How are arguments passed on the stack?

To pass parameters to a subroutine, the calling program pushes them on the stack in the reverse order so that the last parameter to pass is the first one pushed, and the first parameter to pass is the last one pushed. This way the first parameter is on top of the stack and the last one is at the bottom of the stack.


1 Answers

This should suffice:

void displayStack(const Stack<char>& stack);
like image 96
user1610015 Avatar answered Oct 01 '22 00:10

user1610015