I'm just practicing creating a stack class and I'm trying to overload operator=
and operator+
. So far I have successfully overloaded the operator=
.
Let's say my objective is to be able to do st3 = st1 + st2;
operation in the main where st1, st2, st3 are the objects of class stack and assuming they all have the same size and are all int values.
Right now st3 is not returning any values.
In my class I had:
stack& operator+(stack& otherStack){
int sum = 0;
stack res;
while ((!this->isEmpty()) && (!otherStack.isEmpty())) {
sum = this->top() + otherStack.top();
res.push(sum);
this->pop();
otherStack.pop();
}
return res;
}
And here is my print()
function using recursion which is also in class stack
. It prints st1 and st2 just fine
void printStack(){
if (isEmpty())
return;
int x = this->top();
this->pop();
this->printStack();
this->push(x);
}