Pushing an object on a Stack

I keep getting errors every time I try to push the instance of the object instead of an int. Any suggestions on how to get it to compile?

main():
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include "Stack.h"

using namespace std;

class Gumball {
    public:
        string color;
        int counter;
};

int main()
{
    Stack s, gumballStack;
    Gumball g;
    //int topElement;
    int wantedcolor;
    char choice;
    bool choice_flag = true;

    do {
        cin >> choice;
        cin >> g.color;
        switch(choice)
        {
            case 'b':
            case 'B':
                cout << "A" << " " << g.color << " gumball has been bought." << endl << endl;
                g.counter = 0;
                s.isempty();
                s.push(g);
                if(!s.isfull())
                    cout << "The gumball is" << " " << g.color << " and has been stored." << endl << endl;
                else
                    cout << "There is no room for another gumball." << endl << endl;
                break;
            case 'e':
            case 'E':
                while(!s.isempty())
                {
                    s.pop();

                    if(s.pop() != wantedcolor)
                    {
                        gumballStack.push(s.pop());
                        s.pop();
                        g.counter++;
                        cout << "A gumball has been eaten." << endl << endl;
                    }
                    else
                    {
                        g.counter++;
                        cout << "A" << " " << g.color << " was not found." << endl << endl;
                    }
                    while(!gumballStack.isempty())
                    {
                        s.push(gumballStack.pop());
                        gumballStack.pop();
                    }
                }
                break;
            case 'q':
            case 'Q':
                choice_flag = false;
                break;
        }
    } while(choice_flag);

    return 0;
}


.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef STACK_H
#define STACK_H


// Interface file  - Stack class definition
class Stack {
    public:
        Stack();
        void push(Gumball);
        int pop();
        bool isempty();
        bool isfull();
    private:
        int gumballs[6+1];
        int top;
};


#endif // STACK_H 


.cpp file(push/pop functions):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Function to add item x to stack
void Stack::push(Gumball x)
{
    if(!isfull()){
        top++;
        gumballs[top] = x;
        return; }
    else
        return;
}

// Function to remove and return top item of stack
int Stack::pop()
{
    int x;

    if(!isempty()) {
        x = gumballs[top];
        top--;
        return x; }
    else
        return x;
}
Your Stack.h does not know about the Gumball class so it's just assuming it's an int. You should put the Gumball class definition into a .h file, and have Stack.h and your main file #include it. Also, your array of gumballs member in the stack class should be an array of Gumballs, not ints.
Thank you so much ! :-)
Topic archived. No new replies allowed.