error: invalid use of member (did you forget the '&' ?)

I'm getting this error on lines 77 and 84 of intstack.cpp. I spent an hour searching the web but I'm still not sure what to do. Any hints?

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
#pragma once

#include <stdlib.h>
class IntStack
{
    int INITIAL_SIZE;
    int stack_size;
    int* st_arr;
public:
    int stack_capacity;
    //default constructor
    IntStack() : INITIAL_SIZE(100), st_arr(new int [INITIAL_SIZE]) {
        stack_capacity = INITIAL_SIZE;
    }
    //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();
    //int* allocate(int num);
    //void deallocate( int* old);
    bool is_full();
    bool is_empty();
    friend IntStack print_stack(IntStack* pstack);
};


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <stdlib.h>
#include "intstack.h"
#include "intstack.cpp"
using namespace std;

int main()
{
    IntStack stack;
    stack.reset();
    print_stack(&stack);

    cout << stack.stack_capacity;
    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

#include "intstack.h"
#include <iostream>

using namespace std;

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

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

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

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

void IntStack::reset(){
    //loop through the stack and set each element to NULL
    for (int i = 0; i < INITIAL_SIZE; i++){
        st_arr[i] = NULL;
    }
    stack_size = 0;
}

void IntStack::push(int n){
    //if stack is full, int cannot be pushed, signal error
    if (stack_size == 100){
        cerr << "STACK EMPTY\n";
    }
    //set current element of stack to pushed int then increment index
    st_arr[stack_size] = n;
    stack_size++;
}

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

int IntStack::pop(){
    //if stack is empty, int cannot be set to null, signal error
    if (stack_size == 0){
        cerr << "STACK EMPTY\n";
    }
    else
    //decrement to previous stack index then set it to NULL
    --stack_size;
    int temp = st_arr[stack_size];
    st_arr[stack_size] = NULL;
    return temp;
}

void IntStack::pop( int a[], size_t n ){
    stack_size--;
    for (int idx = 0; idx < n; idx++){
            a[idx] = st_arr[stack_size];
            st_arr[stack_size] = NULL;
        if(stack_size>0)stack_size--;
    }
    //for (i = 0; st_arr[i] != NULL; i++);
    //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 < stack_size; 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(stack_size == 100)
        return true;
    else
        return false;
}

bool IntStack::is_empty(){
    if(stack_size != 0)
        return false;
    else
        return true;
Last edited on
You're using capacity as a data member when it's actually a function. You probably meant to use stack_capacity.

By the way, IntStack::allocate() and IntStack::deallocate() shouldn't be member functions of IntStack. They don't use the object at all. At most, they should be static member functions.
Fixed all of that, thanks.

I'm now running into errors saying all of my functions have been previously defined. Something to do with my includes? I can't spot the mistake. intstack.cpp is only included once and I have #pragma once guarding intstack.h
Last edited on
Topic archived. No new replies allowed.