problem with class code

Undo Stack Using Circular Buffer
I have to write a main program that pushes numbers on the stack and pushing more numbers than the stack capacity. I have to 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 something wrong with return myStack.size(); and return myStack.top() = *data;
Last edited on
There is something wrong with return myStack.size(); and return myStack.top() = *data;
Where is data (line 13) initialized?

There is something wrong with return myStack.size();
I don't think so.
1. stack<int> myStack at line 11 is an array of int but you push data of type double to it at line 22 . If you want an array of double then change the declaration of the variable at line 11 to stack<double> myStack.

2. Line 13 double *data has no use

3. Line 28 should only return the myStack.top() and not myStack.top() = *data.
myStack.top() = *data is assigning and replacing the value of the top of myStack with the value pointed to by data. This line myStack.top() = *data can cause also illegal memory access since data is not pointing to a valid memory location since it is not initialized in anywhere of the program. Bottom line, you should only return myStack.top() and not use *data since there is no point.
What should I put in the main function.
I have answered you already. apply that to your code in main.
like this?
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<double> 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();
}
int CircStack::sizeStack() {
    return myStack.size();
}
int main()
{
CircStack myStack(5);
myStack.push(5);
myStack.pops();
myStack.peek();
myStack.sizeStack();
}
Last edited on
Remove line 13 since it is not needed. You have already line 11 which is a stack of type double (meaning an array of type double). At main(), i believe you need to output something for you to verify that you're program is correct like try to print the top after pushing. Print the size before and after pushing and popping....more like debugging prints for you to say that your code is working as expected.
How would I print it?
cout<< <<endl;
Last edited on
Yes. Using cout
1
2
Example:
cout << "Top of the queue: " << myStack.top() << endl;
I got an error when I ran the code cout << "Top of the queue: " << myStack.top() << endl;
Last edited on
what is the error? If you got error put it in here the description of it to save time.

Put all your codes and also the description of the error
Last edited on
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
41
#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<double> 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();
}
int CircStack::sizeStack() {
    return myStack.size();
}
int main()
{
CircStack myStack(5);
myStack.push(5);
myStack.pops();
myStack.peek();
myStack.sizeStack();
cout << "Top of the queue: " << myStack.top() << endl;
}

the error said
top is a private member of CircStack and Called object type in is not a function or function pointer
This line is just an example of debugging lines...it is obvious that you don't understand the error.

 
cout << "Top of the queue: " << myStack.top() << endl;


This line has no point when you just copy it in main, you should put your own debugging code or tracing code for you to verify that your functions are working.

Now let me guide you on how to fix a compile error like this..
 
cout << "Top of the queue: " <<  myStack.top() << endl;


this line obviously causes the error. here is how to interpret the error description...
 
top is a private member of CircStack...


Upon looking at the description, you can then ask yourself, is there a member function or data in CircStack myStack having a name "top"? If the answer is non, then you should not put myStack.top() in the cout line above. Call a function supported by the CircStack class such as push(), pops(), peek(), and sizeStack(). Do you get it?

One more thing, your function CircStack::push() and CircStack::pops() are functions that have return type of type void which means nothing should be returned but you return something. Might as well remove the "return" or put a return type on those functions.
Last edited on
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<double> 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();
}
int CircStack::sizeStack() {
    return myStack.size();
}
int main()
{
CircStack myStack(5);
myStack.push(5);
myStack.pops();
cout << "Top of the queue: " <<myStack.peek()<<endl;
cout << "Size of the queue: " << myStack.sizeStack() << endl;
}

I am not getting any outputs
Line 38 causes program crash. You must not call peek() which gets the top of the stack if the stack is empty. In line 38, stack is empty since you push (insert) 5 at line 36 but you pop (remove) it at line 37. And you try to get and print the top of the stack at line 39. Try to comment out line 38, "Size of the queue: 0" will be outputted.

You have to check first the size of the stack before getting any item from it coz the stack might be empty and you can have a problem.
Last edited on
Still no output after comment out line 38
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<double> 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();
}
int CircStack::sizeStack() {
    return myStack.size();
}
int main()
{
CircStack myStack(5);
myStack.push(5);
myStack.pops();
myStack.peek();
cout << "Size of the queue: " << myStack.sizeStack() << endl;
}

Line 31 there is something saying
Implict conversion losses integer precision:'size_type' (aka 'unsigned long') to 'int'

I am getting lldb in Xcode
Last edited on
I also have to implement the same user interface but using a linked list and add or remove at the head of the list to push or pop.
2 options

1. change the return type of sizeStack() at line 30 into "unsigned long". unsigned long might be the return type of the myStack.size() that you return at line 31. The types should match.

2. cast the value of myStack.size() at line 31 before returning into an int
as below:

return (int)myStack.size();

to convert the number of whatever type returned by myStack.size() at line 31 before returning it.
I try option 2 and I still get lldb in Xcode.
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<double> 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();
}
int CircStack::sizeStack() {
    return (int)myStack.size();
}
int main()
{
CircStack myStack(5);
myStack.push(5);
myStack.pops();
myStack.peek();
cout << "Size of the queue: " << myStack.sizeStack() << endl;
}
Last edited on
Topic archived. No new replies allowed.