I want to push a value to a stack using the assignment operator, I need some help with this.. Thanks

Stack s;
float x = 5;

s = x;

class Stack
{
public:
Stack();
float push(float value);
float pop();
int Size();
float operator = (const float &right);
private:
float values[30];
int top;
int size;
};



float Stack::push(float value)
{
top++;
size++;
values[top] = value;
return value;
}

float Stack::pop()
{
size--;
return values[top--];
}

int Stack::Size()
{
return size;
}

float Stack::operator= (const float &right)
{
float temp_val = right;
Stack temp;
return temp.push(temp_val);
}

In operator= (lines 41 - 46) you create a temporary stack which gets deleted as soon as the function exits. This is surely not what you intend, I imagine you want to push onto the current stack don't you?

You are already in side a member of stack so you can just use the this pointer to access the current object to execute the push operation return this->push(right);

ok one more question. What if i want to do it the other way around. Pop a value into a float varialble.

Stack s;
float x;
x = s;

this is what i have so far..

float Stack::operator= (const Stack &right)
{
    return right.pop();
}

Your problem here is that you are assigning to a float variable

float f;
Stack MyStack;

// Code to populate stack

f = MyStack;

The left hand side is a float but it is the left hand side that determines the class used when calling a member function; clearly you can not set the operator= on the float class, it is built-in and not a class.

Another way to work out that there is a problem is to look what your current method works on, it operates on the current object this and another Stack object which is passed in, that is 2 Stack object which is not what you are after.

The answer is not to use a method at all but rather to use a helper function external to the class, something like this

float operator=(float& left, const Stack& right)
{
    left = right.pop();
    return left;
}

For symetry you could also implement you previous assignment operator as a function rather than a method, noting it is normal to return what was assigned to from the assignment operator

const Stack& operator=(Stack& left, const float& right)
{
    left.push(right);
    return left;
}

Correction on Banfa's last post. You cannot make the assignment operator into a free function, it must be a class member function. So, Banfa's code will not work, although he is very correct in suggesting the free function operator overloading instead of as class member functions, this is generally preferrable (for the operators that are allowed to be overloaded as free functions).

I have a problem with your use of the assignment operator for push-pop operations. The problem is that you shouldn't drastically change the semantics of an operator when creating an overloaded version of it. All programmers generally see the assignment operation as exactly that, an assignment of the contents of the right-hand-side to the variable on the left-hand-side. Overloading it to mean pushing a value on the left-hand-side stack is a bad and confusing idea.

If you absolutely need to use a set of operators for push-pop (btw, what's the problem with just having a "push" and "pop" function?), then you should use the << and >> operators instead (with semantics and chaining behaviour that mimics the iostreams). And those are allowed to be free functions, so you can do this:

Stack& operator<<(Stack& left, float right) {
  left.push(right);
  return left;
};

Stack& operator>>(Stack& left, float& right) {
  right = left.pop();
  return left;
};

This will allow you to create code like this:

int main() {
  float values[] = {0.0,1.0,2.0,3.0};
  for(int i = 0; i < 4; ++i)
    std::cout << values[i] << " ";
  std::cout << std::endl;

  // inverting the array by stacking them one way and popping them the other way:
  Stack s;
  s << values[0] << values[1] << values[2] << values[3];
  s >> values[0] >> values[1] >> values[2] >> values[3];

  for(int i = 0; i < 4; ++i)
    std::cout << values[i] << " ";
  std::cout << std::endl;
  return 0;
};
Comments
Doh, thanks for catching my mistake
This article has been dead for over six months. Start a new discussion instead.