Pointer and parameter problems

Problem defined in comments. Line 53 of main and line 165 of intstack.cpp

intstack.h
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
#pragma once

#include <stdlib.h>
class IntStack
{
    int current_element;
    int* st_arr;
public:
    const static int DEFAULT_STACK_CAPACITY = 100;
    size_t stack_capacity;
    //default constructor
    IntStack() : /*DEFAULT_STACK_CAPACITY(100),*/ st_arr(new int [DEFAULT_STACK_CAPACITY]), stack_capacity(DEFAULT_STACK_CAPACITY), current_element(0)
    {
    }
    //overloaded constructor
    IntStack(int n) : stack_capacity(n), st_arr(new int [DEFAULT_STACK_CAPACITY]), current_element(0)
    {
    }
    //destructor
	~IntStack() {delete [] st_arr;}
    void reset();
    void push(int n);
    void push(int a[], size_t array_size);
    int pop();
    void pop(int a[], size_t n);
    int top();
    size_t size();
    size_t capacity();
    void double_capacity();
    bool is_full();
    bool is_empty();
    void evaluate(std::string com, IntStack* st, int* arr);
    //friend void evaluate(std::string com, IntStack* st);
    friend IntStack print_stack(IntStack* pstack);
};


main.cpp
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
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cassert>
#include "intstack.h"

using namespace std;

void command_list();
//void evaluate(string com, IntStack* st);
IntStack create_stack(char capans, int cap);
int main()
{
    string command;
    //int st_num = 0;

    char capans = 'y';
    int cap;
    cout << "Welcome to the stack program. To begin with, a stack must be created. \n";
    cout << "Would you like to use the default capacity of 100? y/n \n";
    cin >> capans;
    if (capans == 'n'){
        cout << "Enter capacity: \n";
        cin >> cap;
    }
    IntStack st = create_stack(capans, cap);

    int arr_size;
    cout << "Now an array of integers needs to be created. How many integers will this array have? \n";
    cin >> arr_size;
    int arr[arr_size];
    int el;
    for (int i = 0; i < arr_size; i++){
        cout << "Enter element " << i << " of the array \n";
        cin >> el;
        arr[i] = el;
    }

    cout << "Here are your command choices: \n";
    cout << "push     //to push ints to stack \n";
    cout << "pop      //to pop off elements \n";
    cout << "top      //to get top element of stack \n";
    cout << "print    //to print a stack \n";
    cout << "size     //to get the size of a stack \n";
    cout << "capacity //to see the capacity fo a stack \n";
    cout << "reset    //reset stack \n";
    cout << "commands //to display this command list \n";
    cout << "exit     //to exit the program \n\n";
    //int result;
    while(1==1){
        cout << "> ";
        cin >> command;
        st.evaluate(command, &st, &arr);// I want to pass the address of the arr array. giving me a "no matching function call" error


    }
    return 0;
}


intstack.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <string>
#include "intstack.h"
#include <iostream>

using namespace std;

IntStack print_stack(IntStack* pstack){
    for (int i = 0; i < pstack->current_element; i++){
        cout << pstack->st_arr[i] << "\n";
    }
}

int IntStack::top(){
    current_element--;
    int temp = current_element;
    current_element++;
    return st_arr[temp];
}

size_t IntStack::size(){
    return current_element;
}

size_t IntStack::capacity(){
    return stack_capacity;
}

void IntStack::reset(){
    for (int i = 0; i < stack_capacity; i++){
        st_arr[i] = NULL;
    }
    current_element = 0;
}

void IntStack::push(int n){
    if (current_element == stack_capacity){
        cout << "Stack full... doubling capacity \n";
        double_capacity();
    }
    st_arr[current_element] = n;
    current_element++;
}

void IntStack::push( int arr[], size_t array_size ){
    for (int i = 0; i < array_size; i++){
        if (current_element == stack_capacity){
            double_capacity();
        }
        push(arr[i]);
    }
}

int IntStack::pop(){
    if (current_element == 0){
        cerr << "STACK EMPTY\n";
    }
    --current_element;
    int temp = st_arr[current_element];
    st_arr[current_element] = NULL;
    return temp;
}

void IntStack::pop( int a[], size_t n ){
    if (current_element > 0)
        current_element--;
    for (int idx = 0; idx < n; idx++){
        if (current_element == 0){
            cerr << "STACK EMPTY\n";
        }
        a[idx] = st_arr[current_element];
        st_arr[current_element] = NULL;
        if(current_element>0)
            current_element--;
    }
    //cout << "after popping, i is " << i << "\n"; //DB
}

int* allocate(int num);
void deallocate(int* old);

void IntStack::double_capacity(){
    int* new_mem = allocate(stack_capacity * 2);

    for (int i = 0; i < current_element; i++){
        new_mem[i] = st_arr[i];
    }
    deallocate(st_arr);
    st_arr = new_mem;
    stack_capacity *= 2;
}

int* allocate(int num){
    int* new_mem = new int[num];
    return new_mem;
}

void deallocate(int* old){
    delete[] old;
}

bool IntStack::is_full(){
    if(current_element == 100)
        return true;
    else
        return false;
}

bool IntStack::is_empty(){
    if(current_element != 0)
        return false;
    else
        return true;
}

//-------------------------
//---Interface Functions---
//-------------------------

void command_list(){
    cout << "Here are your command choices: \n";
    cout << "push     //to push ints to stack \n";
    cout << "pop      //to pop off elements \n";
    cout << "top      //to get top element of stack \n";
    cout << "print    //to print a stack \n";
    cout << "size     //to get the size of a stack \n";
    cout << "capacity //to see the capacity fo a stack \n";
    cout << "reset    //reset stack \n";
    cout << "commands //to display this command list \n";
    cout << "exit     //to exit the program \n\n";
}

void IntStack::evaluate(string com, IntStack* st, int* arr){
    int c = 9;

    if (com == "push")    { c = 0; }
    if (com == "pop")     { c = 1; }
    if (com == "top")     { c = 2; }
    if (com == "print")   { c = 3; }
    if (com == "size")    { c = 4; }
    if (com == "capacity"){ c = 5; }
    if (com == "reset")   { c = 6; }
    if (com == "commands"){ c = 7; }
    if (com == "exit")    { c = 8; }


    //IntStack st = *st_;
    switch (c)
    {
        case 0:
            char p_ans;
            cout << "Push from array of integers? y/n \n";
            cin >> p_ans;
            if (p_ans == 'y'){
                int num;
                cout << "Push how many ints? \n";
                cin >> num;
                //if (num > )
            }
            int x;
            cout << "Enter int: \n";
            cin >> x;
            st->push(x);
            break;
        case 1:
            cout << "Popped a " << st_arr[current_element] << "/n"; //this gives some garbage value. How do I dereference it here? st->st_arr[current_element] doesn't work
            st->pop();
            break;
        case 2:
            cout << "Top element: " << st->top() << "\n";
            break;
        case 3:
            print_stack(st);
            break;
        case 4:
            cout << "Size: " << st->size() << "\n";
            break;
        case 5:
            cout << "Capacity: " << st->capacity() << "\n";
            break;
        case 6:
            cout << "All values erased \n";
            st->reset();
            break;
        case 7:
            command_list();
            break;
        case 8:
            exit(1);
        case 9:
            cout << "Please enter a valid command \n";
            break;
    }
}

IntStack create_stack(char capans, int cap ){
    if (capans == 'y'){
        IntStack st;
        return st;
    }
    if (capans == 'n'){
        IntStack st(cap);
        return st;
    }
}


Getting pretty close to being finished with this. Would like to get it done by 3 or 4 am tops. Any help is greatly appreciated.
I want to pass the address of the arr array. giving me a "no matching function call" error
The name 'arr' is already the address of the array. You don't need to (nor can) get the address of arr.
Thanks.
Any input on line 165?
Does IntStack::current_element hold the size of the array, or the index of the last element? If the former, it should be st_arr[current_element-1].
Got it, thanks. Always the little things.
Topic archived. No new replies allowed.