Undefined Reference errors...can't seem to find the source

I've been attempting to compile my program to test a few functions out (mind you, it's incomplete) and I've been getting a TON of errors like:
"undefined reference to 'stringArray::func_name(blahblah)'

I can't find what's causing these compiler errors. This is only my second semester with programming and it hasn't happened to me before when I've made header files and implementation files.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdlib>
#include "stringArray.h"

using namespace std;

int main( )
{
   stringArray sa1, sa2(100);
   sa1.addString("abcdefg");
   sa1.addString("abcdefg");
   sa1.addString("lmnopq");
   sa1.printStringArray( );
   sa2.addString("1234");
   return EXIT_SUCCESS;
}


stringArray.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
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "stringArray.h"

// Constructor
stringArray::stringArray (const size_t max_size)
{
	count = 0;
	capacity = max_size;
	// Dynamically creates space for strings in the bag
	data = new stringArray::str[max_size];
}



// Copy constructor
stringArray::stringArray (const stringArray& source)
{
	count = source.count;
	capcity = source.capacity;

	// Maybe call the overload function (=) for a DEEP copy? (TO BE DONE)
	// std::copy(source.data, source.data + source.count, data);

	for (int i = 0; i < capacity; i++)
	{
	    data[i] = source.data[i];
	}
}



// Destructor
stringArray::~stringArray();
{
	// Frees up memory
	// Uses a for loop to delete each element in the bag
	for (int j = 0; j < capacity; j++)
	{
        delete[] data[j];
	}

	// Finally deletes data itself
	delete data;
}



// Adds a string into the bag
// Returns true if successful; false otherwise
bool stringArray::addString(const char new_string[ ])
{
	// Check bounds to see if the bag is filled

	// If it attempts to add the string into the full bag, return false
    if ((count + 1) > capacity)
    {
        return false;
    }

    // Otherwise, return true and add the string into the bag
    else
    {
        // Adds the string into the bag
        data[count] = new_string[];

        // Increases the number of items in the bag
        count++;

        return true;
    }
}



// Removes one occurrence of old_string from the bag
// Returns true if successful; false otherwise
bool stringArray::removeString(const char old_string[ ]);
{
	// Searches for the string
	for (int i = 0; i < capacity; i++)
	{
	    if (data[i] == old_string[])
	    {
	        // Shift all of the elements to the left
	        for (int index = i; index < capacity; index++)
	        {
                data[index] = data[index+1];
	        }

            // Reduces count by 1
            count = count - 1;
            return true;
	    }
	}

	// Returns false if the string cannot be found/removed
	return false;
}



// Removes all occurrences of old_string from the bag
// Returns number of strings removed
size_t stringArray::removeAll(const char old_string[ ])
{
    // Counter variable to return at the end
    size_t counterRemoved = 0;

    // Searches through the object for the string
    for (int i = 0; i < capacity; i++)
    {
        if (data[i] == old_string[])
	    {
	        // Shift all of the elements to the left
	        for (int index = i; index < capacity; index++)
	        {
                data[index] = data[index+1];
	        }

            // Reduces count by 1 and increments the counter
            count = count - 1;
            counterRemoved = counterRemoved + 1;
	    }
	}

    return counterRemoved;
}



// Substitutes/replaces ONE occurrence of old_string with new_string
// Returns true if successful; false otherwise
bool stringArray::substitute(const char old_string[ ], const char new_string[ ])
{
    // Searches through the object
    for (int i = 0; i < capacity; i++)
    {
        if (data[i] == old_string[])
        {
            // Replace the old_string[] with the new_string[]
            data[i] = new_string[];

            // Returns true since the string has been replaced
            return true;
        }
    }

    return false;
}



// Substitutes/replaces ALL occurrences of old_string with new_string
// Returns number of strings replaced
size_t stringArray::substituteAll(const char old_string[ ], const char new_string[ ])
{
    // Counter variable
    size_t count = 0;

    // Searches through the object
    for (int i = 0; i < capacity; i++)
    {
        if (data[i] == old_string[])
        {
            // Replaces and increments count
            data[i] = new_string[];
            count++;
        }
    }

    // Returns the number of strings replaced
    return count;
}



// Returns total number of strings in the bag
size_t stringArray::size( ) const
{
	return count;
}



// Returns true if cur_string is in the bag; false if not
bool stringArray::is_string(const char cur_string[ ]) const
{
    // Searches through the object
    for (int i = 0; i < capacity; i++)
    {
        if (data[i] == cur_string[])
        {
            // Returns true if the string is found in the object
            return true;
        }
    }

    return false;
}



// Returns number of occurrences of cur_string
size_t stringArray::occurrences(const char cur_string[ ]) const
{
    // Counter to determine how many occurrences to return
    size_t counter = 0;

    // Searches through the object
    for (int i = 0; i < capacity; i++)
    {
        if (data[i] == cur_string[])
        {
            // Increment the counter by 1
            counter = counter + 1;
        }
    }

    return counter;
}



// Prints out all strings in the object
// One string per line
void stringArray::printStringArray( ) const
{
    for (int i = 0; i < capacity; i++)
    {
        cout << data[i] << "\n";
    }
}



// Prints all strings out in the object
// Sorted order from least to greatest
void stringArray::printSortedStringArray( ) const
{
    // TO BE DONE
}


stringArray.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
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
#ifndef STRINGARRAY_H_INCLUDED
#define STRINGARRAY_H_INCLUDED

class stringArray
{
    public:
        // Constructor and copy constructor
        stringArray (const size_t max_size = DEFAULT_SIZE);
        stringArray (const stringArray& source);
        // Destructor
        ~stringArray();

        // Adds a new string in the bag
        // Returns true if successful; false otherwise
        bool addString(const char new_string[ ]);

        // Removes one occurrence of old_string from the bag
        // Returns true if successful; false otherwise
        bool removeString(const char old_string[ ]);

        // Removes all occurrences of old_string from the bag
        // Returns number of strings removed
        // May need to be changed to an int
        size_t removeAll(const char old_string[ ]);

        // Substitutes/replaces one occurrence of old_string with new_string
        // Returns true if successful; false otherwise
        bool substitute(const char old_string[ ], const char new_string[ ]);

        // Substitutes/replaces all occurrences of old_string with new_string
        // Returns number of strings replaced
        size_t substituteAll(const char old_string[ ], const char new_string[ ]);

        // Returns total number of strings in the bag
        size_t size( ) const;

        // Returns true of cur_string is in the bag; false if not
        bool is_string(const char cur_string[ ]) const;

        // Returns number of occurrences of cur_string
        size_t occurrences(const char cur_string[ ]) const;

        // Prints out all strings in the object
        // One string per line
        void printStringArray( ) const;

        // Prints all strings out in the object
        // One string per line
        // Sorted order from least to greatest
        void printSortedStringArray( ) const;

        // Sets the default_size/capacity of the bag to 20
        static const size_t DEFAULT_SIZE = 20;

        // Typedef stuff
        typedef char* str;

    private:
        // count = The number of items
        size_t count;
        // capacity = The number of items that can be stored
        size_t capacity;
        // data = Item
        str* data;
};

#endif // STRINGARRAY_H_INCLUDED 
Last edited on
I tried compiling this code and got all sorts of errors. Many of which you should be able to figure out on your own.

But here we go!

stringArray.h
line 57: you're not const correct. you probably want to change this typedef to be const char* instead of char*. Although you have deeper problems. See below.

stringArray.cpp
line 21: you misspelled capacity
line 35: you shouldn't have a semicolon there
line 66: you can't have empty brakets like that. Just = new_string;
line 79: shouldn't have a semicolon
line 84: again with the empty brakets. You can't have empty brakets.
line 114: ditto
line 140: again
line 143: "
line 165, 168, 193: more of the same
line 209: This line makes no sense. Is that + supposed to be a =?
line 214: empty brakets
line 232: you're using cout without being in std namespace. Either use std::cout or put in using namespace std

Deeper problems

CHAR POINTERS ARE NOT STRINGS

You should just use std::string.
Line 209 - Sorry, that's supposed to be size_t counter = 0;. I guess I was half-asleep when I was doing this in the morning.

Thanks for replying though. I'm confused as to why line 57 in stringArray.h needs to be a const char* and not just char*.

We're not allowed to use strings for this assignment (Which is dumb). I definitely WOULD if I could though.

Fixed all of those problems though, but I'm still getting "Undefined Reference" errors...
Thanks for replying though. I'm confused as to why line 57 in stringArray.h needs to be a const char* and not just char*.


Because of this:

1
2
3
4
5
bool stringArray::addString(const char new_string[ ])  // <- new_string is const
{
//...
        data[count] = new_string;  // <- can't convert a const pointer to a nonconst pointer
   // therefore, data must be a const pointer 


We're not allowed to use strings for this assignment (Which is dumb).


I agree.

Fixed all of those problems though, but I'm still getting "Undefined Reference" errors...


I'm not. Once I fix all those problems it compiles and links fine (although I do get some warnings about comparing signed/unsigned types)

It would help if you posted the full error message instead of just summarizing it.
can u kindly post those errors here?
Well, here are my compiler errors...

1
2
3
4
5
6
7
8
9
10
Line 9 - undefined reference to 'stringArray::stringArray(unsigned int)'
Line 9 - undefined reference to 'stringArray::stringArray(unsigned int)'
Line 10 - undefined reference to 'stringArray::addString(char const*)'
Line 11 - undefined reference to 'stringArray::addString(char const*)'
Line 12 - undefined reference to 'stringArray::printStringArray() const'
Line 13 - undefined reference to 'stringArray::addString(char const*)'
Line 15 - undefined reference to 'stringArray::~stringArray()'
Line 15 - undefined reference to 'stringArray::~stringArray()'
Line 15 - undefined reference to 'stringArray::~stringArray()'
Line 15 - undefined reference to 'stringArray::~stringArray()'


Sometimes, I think it's my compiler that's hating me and causing all of these errors, but at the same time...I really don't know.
Are you even compiling stringArray.cpp? Are you linking to it?

What IDE are you using?
I am linking to it. I'm using Code::Blocks.

Again, I haven't had these errors before and I've used implementation/header files waaaay before this assignment and they've worked fine.
I am linking to it.


I really don't think you are. I suspect you didn't check the "add file to active project" checkbox or the "debug/release" checkboxes (which are strangely unchekced by default in C::B) when you created that cpp file.

In the project manager in the left column thing:
- right click on stringArray.cpp
- go to properties.
- go in the "Build" tab
- make sure "Compile File" and "Link File" are both checked
- make sure the "Debug" and "Release" boxes in "Belongs in Targets" are both checked

Then rebuild.
I'm dumb. I thought I linked those, but I guess not. I definitely owe you one, Disch. Thank you!
Topic archived. No new replies allowed.