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.
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.
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.
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.
This should suffice:
void displayStack(const Stack<char>& stack);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With