Pushing all elements from one stack to another stack without the stacks being pre-declared

Hi. Just as the title says, I want to create a function that pushes all elements from one stack to another when it is called. I already have a prototype, but it only works with two pre-declared stacks. Here it is:
1
2
3
while (!s1.isEmpty()) {
s2.push(s1.peek());
s1.pop();


I need to somehow have this function take stacks as arguments, not sure if one or two stack. I imagined it something like stack1.pushToStack2() when being called. Is it possible to achieve this? The two stacks will only be declared in main. Or if it doesn't work like that, maybe push(stack1, stack2), to have it take two stacks as arguments and transfer from the first to the second? How can I achieve this?

PS: When I say it without them being pre-declared, I mean without having to declare them in my Stack class, but to be able to declare them only in main.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <stack>

void transfer(std::stack<int>& a, std::stack<int>& b) {
    while (!a.empty()) {
        b.push(a.top());
        a.pop();
    }
}

void print_stack(std::stack<int> st) { // pass a copy so we don't destoy the original
    while (!st.empty()) {
        std::cout << st.top() << ' ';
        st.pop();
    }
    std::cout << '\n';
}

int main() {
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    std::stack<int> x, y;
    for (int i = 0; a[i] > 0; i++)
        x.push(a[i]);
    print_stack(x);
    transfer(x, y);
    print_stack(y);
    return 0;
}

Hi. Thanks for replying. I forgot to mention I'm using my own Stack class, pretty similar to <stack> but it is a template class with <typename T, int N> which are the type of elements to add to the stack and the size of the stack. I put your transfer function in my Stack class, changed it accordingly and when I try to call transfer(stack1, stack2) I get the error: implicit declaration of function int transfer(...). What am I doing wrong? Here is the function below:
1
2
3
4
5
6
template<typename T, int N>
void Stack<T, N>::transfer(Stack<int, N>& a, Stack<int, N>& b) {
    while (!a.empty()) {
        b.push(a.peek());
        a.pop();
    }
If it's going to be a member function then it only needs to take the "other" stack as a parameter. So maybe something like this:
1
2
3
4
5
6
7
template<typename T, int N>
void Stack<T, N>::transfer_to(Stack<int, N>& other) {
    while (!empty()) {
        other.push(peek());
        pop();
    }
}


Call it something like this:
 
    x.transfer_to(y);


This will probably have the same problem as the other one, but give it a try. If it still doesn't work, post the function and also the line you use to call it.
Ok I managed to get past that, but now I get this error:
no matching function for call to Stack<int, 5>::transfer(Stack<int, 8> &)
candidates are: void Stack<int, 5>::transfer(Stack<int, 5> &)

This seems to be because of the different N's of the two stacks. So basically I have two stacks with two different sizes and it won't work because it needs to have the same N. How can I work around this? Here are the stack declarations and function call:
1
2
3
4
Stack<int, 8> bucket1;
Stack<int, 5> bucket2;

bucket2.transfer(bucket1);
Last edited on
To pass two different types you need to make the function a non-member function. Something like this:
1
2
3
4
5
6
7
8
9
template<typename T, int N, int M>
void transfer(Stack<T, N>& st, Stack<T, M>& other) {
    while (!st.empty()) {
        if (other.full())   // make sure you don't overflow other
            break;
        other.push(st.peek());
        st.pop();
    }
}

I thought non-member functions are defined outside the class with Classname::functionname. What you sent is a member function?
No. Member functions are usually declared "outside of the class" and use the Classname::functionname syntax in order to indicate that they are indeed member functions (of Classname in fact).

So the function above is a non-member function since it doesn't have the Classname:: part. And it of course needs to be declared "outside" of the class.
Last edited on
Also, if I use the code you sent, don't I have to change every single template to <typename T, int N, int M> and they all will expect 3 arguments? You gave parameters to your transfer function that only require 2 arguments. So your template is of typename T, int N, int M but your Stacks are only <T, N> and <T, M>. This is returning errors for me. How is that possible?
Oh shit! It worked. I understand what you meant now and it finally worked. Thank you very much for helping!
No problem. Good luck!
Topic archived. No new replies allowed.