Undefined Reference

This is my code and I am not sure where the problem lies. I have researched and racked my brain and I am sure it is a simple thing but for the life of me I can't find it. I finally got everything else fixed and this is all that is left. Sorry it is a long program.

We were to implement our code from the List we had already created

Here is my main code:

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
#include "List.h"
#include "Stack.h"

#include <fstream>
#include <iostream>
#include <ostream>
#include <string>



using namespace std;

ofstream outfile;
ifstream infile;


int main()

  {
    outfile.open("Stackout.txt");  //open outfile
    if (!outfile)                // check outfile
    {
        outfile << "Output file did not open.\n";
        return 1;
    }
    infile.open("Stackin.txt");
    if (!infile)
    {
        outfile << "Input file did not open.\n";
        return 1;
    }

    string expression;

    Stack A;

    while (!infile.eof())
    {
        A.Inputs();

        outfile << "Input: \n";
        A.Output();

        if (A.Balance())
        {
            outfile << "This Set Has ALL Matching Open and Closed.\n";
        }
        else
        {
            outfile << "This Set DOES NOT Have Matching Open and Closed.\n";
        }
    }

  	return 0;
  }


Here is my List.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "List.h"
#include "Stack.h"

#include <iostream>

using namespace std;

bool operator ==(List &x, List &y);

List::List()
{
    curr_pos = 0;
    t_size = 0;
    check = 0;
}

List::List(List & x)
{
    x.front();

    for(int i = 0; i < x.size(); i++)
    {
        myArray[i] = x.getElement();
        x.next();
    }

    t_size = x.size();

    curr_pos = 0;
    check = 0;
}

bool List::empty() const
{
    if (t_size == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void List::front()
{
    curr_pos = 0;
    check = 0;
}

void List::end()
{
    curr_pos = t_size;
    check = 0;
}

void List::prev()
{
   curr_pos--;
}

void List::next()
{
    curr_pos++;
}

int List::getPos() const
{
    return curr_pos;
}

void List::setPos(int x)
{
    curr_pos = x;
}

void List::insertBefore(eletype x)
{
    if (curr_pos == (t_size - 1) && check == 0)
    {
        curr_pos = CAPACITY;
    }

    myArray[(curr_pos - 1)] = x;

    curr_pos--;

    if (t_size != CAPACITY)
    {
        t_size++;
    }

    check = 1;
}

void List::insertAfter(eletype x)
{
    if (curr_pos == 0 && check == 0)
    {
        curr_pos = -1;
    }

    myArray[(curr_pos + 1)] = x;

    curr_pos++;

    if (t_size != CAPACITY)
    {
        t_size++;
    }

    check = 1;
}

eletype List::getElement()
{
    if (curr_pos < 0)
    {
        curr_pos = 0;
    }
    else if (curr_pos > (t_size - 1))
    {
        curr_pos = t_size - 1;
    }

    return myArray[curr_pos];
}

int List::size() const
{
    return t_size;
}

void List::replace(eletype x)
{
    myArray[curr_pos] = x;
}

void List::erase()
{
    myArray[curr_pos] = NULL;
}

void List::clear()
{
    for (int i = 0; i < t_size; i++)
    {
        myArray[i] = 0;
    }
}

void List::reverse()
{
    int hold, hold2;

    hold2 = t_size - 1;

    for(int i = 0; i < (t_size / 2); i++)
    {
        hold = myArray[i];
        myArray[i] = myArray[hold2];
        myArray[hold2] = hold;

        hold2--;
    }
}

void List::swap(List & x)
{
    int hold;

    x.front();

    for(int i = 0; i < t_size; i++)
    {
        hold = myArray[i];
        myArray[i] = x.getElement();
        x.replace(hold);

        x.next();
    }
}

ostream& operator <<(ostream& output, List& x)
{
    x.front();

    for (int i = 0; i < x.size(); i++)
    {
        output << endl << "  " << x.getElement();
        x.next();
    }

    output << endl;

    return output;
}

List operator +(List &x, List &y)
{
    List z;

    x.front();
    y.front();

    for (int i = 0; i < y.size(); i++)
    {
        z.replace(x.getElement() + y.getElement());

        x.next();
        z.next();
        y.next();
    }

    return z;
}

List& List::operator =(List &x)
{
    x.front();

    for (int i = 0; i < x.size(); i++)
    {
        myArray[i] = x.getElement();

        x.next();
    }

    t_size = x.size();
}

bool operator ==(List & x, List &y)
{
    int check = 0;

    x.front();
    y.front();

    if (x.size() == y.size())
    {
        for (int i = 0; i < x.size(); i++)
        {
            if (x.getElement() != y.getElement())
            {
                check = 1;
            }

            x.next();
            y.next();
        }
    }
    else
    {
        check = 1;
    }

    if (check == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Here is my Stack.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 "Stack.h"
#include "List.h"

#include <iostream>

using namespace std;

typedef char eletype;
int const CAPACITY = 20;

const eletype LP = '(';
const eletype RP = ')';
const eletype LB = '[';
const eletype RB = ']';
const eletype LA = '{';
const eletype RA = '}';
const eletype LC = '<';
const eletype RC = '>';



void Stack::Inputs()
{
    string expression;

    getline(infile, expression);

    size = expression.length();

    for (int i = 0; i < size; i++)
    {
        NewArray[i]= expression[i];
    }
}

void Stack::Push(eletype x)
{
    MyStack.insertAfter(x);
}

void Stack::Pop()
{
    MyStack.erase();
    MyStack.prev();
}

bool Stack::EmptyCheck()
{
    return MyStack.empty();
}

int Stack::StackSize()
{
    return MyStack.size();
}

eletype Stack::Top()
{
    return MyStack.getElement();
}

bool Stack::Balance()
{
    bool failed = false;
    eletype hold;

    for (int i = 0; !failed && (i < size); i++)
    {
        hold = NewArray[i];

        if (hold == LP || hold == LB || hold == LA || hold == LC)
        {
            Push(hold);
        }
        else if ((hold == RP && Top() == LP)
                    && (!EmptyCheck()))
        {
            Pop();
        }
        else if ((hold == RB && Top() == LB)
                    && (!EmptyCheck()))
        {
            Pop();
        }
        else if ((hold == RA && Top() == LA)
                    && (!EmptyCheck()))
        {
            Pop();
        }
        else if ((hold == RC && Top() == LC)
                    && (!EmptyCheck()))
        {
            Pop();
        }
        else
        {
            failed = true;
        }
    }

    return (!failed);
}

void Stack::Output()
{
    for (int i = 0; i < size; i++)
    {
        outfile << NewArray[i];
    }

    outfile << endl;
}


And here are the header files for both:

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
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED



#include <iostream>
typedef char eletype;

class Stack
{
    private:
    List MyStack;
    eletype NewArray[CAPACITY];
    int size;

  public:
    void Inputs();
    void Push(eletype x);
    void Pop();
    bool EmptyCheck();
    int StackSize();
    eletype Top();
    bool Balance();
    void Output();
};

#endif // STACK_H_INCLUDED


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
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED



#include <iostream>
typedef char eletype;

class Stack
{
    private:
    List MyStack;
    eletype NewArray[CAPACITY];
    int size;

  public:
    void Inputs();
    void Push(eletype x);
    void Pop();
    bool EmptyCheck();
    int StackSize();
    eletype Top();
    bool Balance();
    void Output();
};

#endif // STACK_H_INCLUDED 


And I apologize here is the error messages I get

E:\Stack\main.o:main.cpp|| undefined reference to `Stack::Inputs()'|
E:\Stack\main.o:main.cpp|| undefined reference to `Stack::Output()'|
E:\Stack\main.o:main.cpp|| undefined reference to `Stack::Balance()'|
E:\Stack\main.o:main.cpp:(.text$_ZN5StackC1Ev[Stack::Stack()]+0xd)||undefined reference to `List::List()'|
||=== Build finished: 4 errors, 0 warnings ===|

Any Ideas where I am going wrong:

This is the input,its quite simple input:

()
[](){}
[{()}]
<><<<>>>
[[))
{()[()]}


Last edited on
I'm not seeing the header file for list.h. It appears you posted stack.h twice.

The first three error messages seem to imply that stack.cpp did not get compiled.
The fourth error message seems to imply that list.cpp did not get compiled.

Are both list.cpp and stack.cpp in your project?

I'm sorry I didn't realize it, I was getting ready to leave out the door when I got the update. Here it is:



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
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

#include <string>
#include <ostream>
#include <iostream>

typedef char eletype;
int const CAPACITY = 20;

class List
{
  public:
    List();
    List(List & x);
    bool empty() const;
    void front();
    void end();
    void prev();
    void next();
    int getPos() const;
    void setPos(int);
    void insertBefore(eletype);
    void insertAfter(eletype);
    eletype getElement();
    int size() const;
    void replace(eletype);
    void erase();
    void clear();
    void reverse();
    void swap(List & x);
    friend std::ostream& operator <<(std::ostream& output, List &x);
    friend List operator +(List &x, List &y);
    List &operator =(List &x);

  private:
    int curr_pos, t_size, check;
    eletype myArray[CAPACITY];

};

#endif // LIST_H_INCLUDED 


I quite sure it is something that I am doing wrong,. I usually just turn my code in a large continuous file but am trying to develop better habits in programming and make it easier to read as well by dividing it into separate. files. I apologize for the mistake.
Are both list.cpp and stack.cpp in your project?
I second this. It looks like you're only compiling main.cpp.
Topic archived. No new replies allowed.