Undo Stack Using Circular Buffer
Write a main program that pushes numbers on the stack, pushing more numbers than the stack capacity. Test all the methods of the class.
This is what I have so far:
#include <iostream>
#include <stack>
usingnamespace std;
class CircStack {
public:
CircStack(int cap); // parameter is stack capacity
void push(double d); // push new number on stack
void pops(); //remove and return a double
double peek(); // return stack top without removing
int sizeStack(); // return how many elements on stack
stack<int> myStack;
private:
double *data; // pointer to array of doubles
int capacity ; // the size of the array
int top; // index of next open spot
int number; // number of items on stack
};
CircStack::CircStack(int cap) {
cap = capacity;
}
void CircStack::push(double data) {
return myStack.push(data);
}
void CircStack::pops() {
return myStack.pop();
}
double CircStack::peek() {
return myStack.top() = *data;
}
int CircStack::sizeStack() {
return myStack.size();
}
int main()
{
CircStack myStack(5);
myStack.push(5);
myStack.pops();
myStack.peek();
myStack.sizeStack();
}
There is error in return myStack.size(); and return myStack.top() = *data;
can you use code tags in the future please? i can hardly read that.
anyway.. your issue is that you've defined a constructor which means your compiler will not provide a default one. Seeing as yours takes an int i changed you main to this:
1 2
CircStack myStack(5); // <-- i chose an arbritray int
myStack.push(5); myStack.pops(); myStack.peek(); myStack.sizeStack();
and it compiles.
edit: dont do this: int capacity = 4; // the size of the array
initialise it in your constructor. you are attempting to do tihs but i think you want
capacity = cap;
rather than the other way around.
you also then do not use this capacity anywhere but i assume that will come later?
as a side note, i wouldnt put all these statements on one line: CircStack myStack(); myStack.push(5); myStack.pops(); myStack.peek(); myStack.sizeStack();
edit2: and this line: return myStack.top() = *data;
will cause your program to crash. i have no idea why you have a pointer in your class.