Stack issues

sdfsdfsdf
Last edited on
void push (frac frac1 );

declares a function named "push" which takes a frac as parameter and returns void.
It does not actually do anything.

Your stack maintains a stack of ints, but you want to put fracs there.

Stack::pop is unsafe, in that bad things happen if you call pop on an empty stack.

Stack::push is unsface, in that bad things happen if you call push on a full stack.


sdfsdf
Last edited on
You are intending that

void push (frac frac1);

calls the push method on a stack object. It does not. It declares a function. You need to correct these three lines of code to actually call the method on s1. Once you do that, you will get compile errors because your Stack::push method takes an integer as parameter and you are passing a frac. You need to change Stack to work with frac's not ints.

After you've done that, consider what would happen if the user called push() on a stack that already had MAX elements in it. Likewise if the user called pop() with 0 elements in it. You might want to consider handling those cases in some way.

dsfgg
Last edited on
asdfsdf
Last edited on
Three things jump out:

1) Stack::top is meant to be an index into the array. Array indices in C/C++ have to be of an integral type, but you've defined it to be a frac.

2) s1:push(frac1); is not valid syntax; the colon should be a dot.

3) push still takes an int and pop still returns an int, but you want to push and pop fracs.
asdfsdf
Last edited on
sdfdsf
Last edited on
Well, you wrote the code for the Stack class, so you should know.

Your Stack::pop method appears to do two things:
1) Remove the topmost element;
2) Return a copy of the element removed.

So you could do:

1
2
frac temp = s1.pop();
temp.displayfrac();


or you could elide the temporary and make it one line of code

 
s1.pop().displayfrac();

Topic archived. No new replies allowed.