Class function help

I am working on an assignment for class and we are working with class and pointers. I think I have most of the program written but I am getting some errors - and I am not sure which part of the code the issue is, usually when it shows an error it points to a specific line but it isn't currently the case. Below are my errors and then I will post my codes

main.cpp:(.text+0x1d9): undefined reference to `Word::~Word()'
main.cpp:(.text+0x3cd): undefined reference to `Word::Word(char const*)'
main.cpp:(.text+0x716): undefined reference to `Word::GetFirstLetterLower()'
main.cpp:(.text+0x754): undefined reference to `Word::~Word()'
main.cpp:(.text+0x870): undefined reference to `Word::GetWord()'
[Error] ld returned 1 exit status

Word.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 #ifndef WORD_H
#define WORD_H
#include<string>
#include<iostream>

using namespace std;

class Word
{
	private:
		char* ptr_;
		int len_;
	public:
		Word(const char* word);
		~Word();
		char GetFirstLetterLower();
		char* GetWord();
};

#endif 


Word.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
 #include <iostream>
#include "string.h"
#include "Word.h"

using namespace std;

Word::Word(const char* word)
{
	
    len_ = strlen(word);
    ptr_ = new char [len_+1];
    strncpy(ptr_, word, len_);
    ptr_[len_] = 0;
}
Word::~Word()
{
	if(ptr_ != 0)
	{
		delete [] ptr_;
		ptr_ = 0;
		len_ = 0;
	}
}
char Word::GetFirstLetterLower()
{
	char firstLetter = 0;
	if(ptr_ != 0)
	{
		firstLetter = tolower(ptr_[0]);
	}
	return firstLetter;
}
char* Word::GetWord()
{
	char *tempPtr;
	if(ptr_ != 0)
	{
		tempPtr = ptr_;
	}
	return tempPtr;
}


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
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
  #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Word.h"

using namespace std;

const int FILE_PATH_SZ = 512;
Word** g_wordArray;
int  g_arrSz;

char DisplayMenu();
void FreeWordArray(); 
void LoadTextFile();
int CountNumWordsInFile(char filePath[]);
void RemoveWordsStartingWithLetter();
void PrintWords();

int main()
{
	bool terminate = false;
	char selection;
	
	g_arrSz     = 0;
	g_wordArray = 0;
	
	while(!terminate)
	{
		selection = DisplayMenu();
		switch(selection)
		{
			case'a':
				LoadTextFile();
				break;
			case'b':
				RemoveWordsStartingWithLetter();
				break;
			case'c':
				PrintWords();
				break;
			case'd':
				terminate = true;
				break;
			default:
				cout << "That was not a valid choice!";
		}
	}
}
char DisplayMenu()
{
	char selection = '1';
	
	cout << "Please make a section: " << endl;
	cout << "a) Read a text file" << endl;
	cout << "b) Remove words starting with letter" << endl;
	cout << "c) Display words to console" << endl;
	cout << "d) Quit!" << endl;
	
	cin >> selection;
	return tolower(selection);
}
void FreeWordArray()
{
	if(g_wordArray != 0)
	{
		for(int i = 0; i < g_arrSz; ++i)
		{
			if(g_wordArray[i] != 0)
			{
				delete g_wordArray[i];
			}
		}
		delete []g_wordArray;
		g_arrSz     = 0;
		g_wordArray =  0;
	}
}
void LoadTextFile()
{
	const int sz = 256;
	char filePath[sz];
	char temp[sz];
	int wordCnt = 0;
	ifstream inFile;
	
	_flushall();
	cout << "Enter full file path: ";
	cin.getline(filePath, sz);
	
	if((wordCnt = CountNumWordsInFile(filePath)) == 1)
	{
		return;
	}
	
	FreeWordArray();
	
	g_wordArray = new Word*[wordCnt];
	
	if(g_wordArray == 0)
	{
		cout << "Memory Allocation failed" << endl;
		return;
	}
	for (int i = 0; i < wordCnt; ++i)
	{
		g_wordArray[i] = 0;
	}
	
	inFile.open(filePath);
	
	while(!inFile.eof())
	{
		inFile >> temp;
		
		if(temp[0] == 0)
		{
			break;
		}
		
		g_wordArray[g_arrSz] = new Word(temp);
		if(g_wordArray[g_arrSz] == 0)
		{
			cout << "Memory Allocation Failed" << endl;
			break;
		}
		g_arrSz++;
	}
	inFile.close();
	
	cout << "Loaded file and read " << wordCnt << " words." << endl;
}
int CountNumWordsInFile(char filePath[])
{
	const int sz = 256;
	int wordCnt = 0;
	char temp[sz];
	ifstream inFile;
	inFile.open(filePath);
	
	if(!inFile.is_open())
	{
		cout << "File can not be found: " << filePath << endl;
		return -1;
	}
	while(!inFile.eof())
	{
		inFile >> temp;
		if (temp[0] = 0)
		{
			break;
		}
		wordCnt++;
	}
	inFile.close();
	return wordCnt;
}
void RemoveWordsStartingWithLetter()
{
	char chr;
	int wordsRemoved = 0;
	
	_flushall();
	
	if(g_wordArray == 0 || g_arrSz < 1)
	{
		cout << "There are no words in the array" << endl;
		return;
	}
	cout << "Enter the starting letter: ";
	cin >> chr;
	
	chr = tolower(chr);
	
	for(int i = 0; i < g_arrSz; i++)
	{
		if (g_wordArray[i] != 0 && g_wordArray[i] -> GetFirstLetterLower() ==  chr)
		{
			delete g_wordArray[i];
			g_wordArray[i] = 0;
			wordsRemoved++;
		}
	}
	cout << "Removed " << wordsRemoved << " words." << endl;
}
void PrintWords()
{
	cout << endl;
	if(g_wordArray == 0 || g_arrSz < 1)
	{
		cout << "There are no words in the array" << endl;
		return;
	}
	for(int i = 0; i < g_arrSz < 1; i++)
	{
		if(g_wordArray[i] != 0)
		{
			cout << g_wordArray[i] -> GetWord() << " ";
		}
	}
	cout << endl;
}


Before we wrote the program in a single file and it was easier to figure out where the problems were, but now I have no idea where the issue might be. Per my instructor, that erro is because you have those function declared in the .h file but not define in the .cpp file. Which .cpp file would i need to define them? Any pointers or help would be appreciated
Your problem looks like a linking issue. How are you linking your program's modules?
As koothkeeper implied, those are linker errors, not compile errors.

Per my instructor, that error is because you have those function declared in the .h file but not defined in the .cpp file.

That's not the full answer. You have those functions defined in your word.cpp file. The problem is that when you link your program, the linker is not finding your word.o object file, perhaps because you didn't tell the compiler where to look for it.

You didn't say what compiler you're using, so can't give you more specific advice.

Last edited on
I am using Dev C++ and then when I have to verify that it works in linux before turning it in - and noticed that Dev C++ does a good job in it.

This is my first time trying to link different files so having too much issues. Do I need to include the word.cpp file also in the main.cpp to get it to work?
word.cpp needs to be a part of your project, just like main.cpp is. Otherwise, as far as your project is concerned it is just some random unrelated file.
Do I need to include the word.cpp file also in the main.cpp to get it to work?

No. Don't use #include with a .cpp file.

You are thinking along the right lines though.

In DevC++ (the Orwell version I hope!)
You need to start a new project and then add Word.cpp to the project. (Main.cpp should be added by default).

edit:
If you use compiler options:
-std=c++11 -pedantic  -Wall -Wextra

You should get at least a couple of warning messages which are genuinely useful in highlighting problems in your code.
Last edited on
Chervil thank you very much. That helped solve my problem, didn't think it would be such an simple fix. Appreciate the help very much
Topic archived. No new replies allowed.