undefined reference to...

I'm getting the following error: undefined reference to 'IntStack::allocate(int)'
on line 72 of the intstack.cpp file.


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
#include <stdlib.h>
class IntStack
{
    int INITIAL_SIZE;
    int stack_size;
    int* st_arr;
public:
    int capacity;
    //default constructor
    IntStack() : INITIAL_SIZE(100), st_arr(new int [INITIAL_SIZE]) {
        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();
    int size();
    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
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
#include <iostream>
#include <stdlib.h>
#include "intstack.h"
using namespace std;

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

    std::cout << "pushing 11\n";
    stack.push(11);
    std::cout << "is_empty: " << stack.is_empty() << "\n";
    std::cout << "top: " << stack.top() << "\n";
    std::cout << "pop: " << stack.pop() << "\n";
    std::cout << "is_empty: " << stack.is_empty() << "\n";

    std::cout << "pop: " << stack.pop() << "\n"; // this should result in "STACK EMPTY" output, or similar message
    std::cout << "pop: " << stack.pop() << "\n"; // this should result in "STACK EMPTY" output, or similar message
    std::cout << "is_empty: " << stack.is_empty() << "\n";

    std::cout << "pushing 1\n";
    stack.push(1);
    std::cout << "pushing 2\n";
    stack.push(2);
    std::cout << "pushing 3\n";
    stack.push(3);
    std::cout << "pushing 4\n";
    stack.push(4);
    std::cout << "pop: " << stack.pop() << "\n";
    std::cout << "pop: " << stack.pop() << "\n";
    std::cout << "top: " << stack.top() << "\n";
    std::cout << "pop: " << stack.pop() << "\n";


    std::cout << "stack size: " << stack.size() << "\n";

    stack.reset();

    int arr[] = {0, 1, 2, 3, 4};

    stack.push(arr, 5);
    print_stack(&stack);
    stack.pop(arr, 5);

    std::cout << "stack size: " << stack.size() << "\n";

    for(int i = 0; i < 5; ++i)
    {
        std::cout << arr[i] << "\n";
    }

    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
#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];
}

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

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
}

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

    for (int i = 0; i < stack_size; i++){
        new_mem[i] = st_arr[i];
    }
    deallocate(st_arr);
    st_arr = new_mem;
    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;
}


Does it have something to do with an include directive?
You forgot to put IntStack:: in front of the implementation:)
Oh boy, haha. I'll blame that one on being tired. Thanks.
Topic archived. No new replies allowed.