Have trouble with this program

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:
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
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <stack>
using namespace 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;
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.