undefined reference to class::function

Hey guys, if you could take a quick look at this code that would be great. I've spent a few days staring at it and I can't seem to find what is wrong. The error I get is
1
2
3
In function 'add(std::string, std::string, std::string, std::string, std::string)
main.cpp(.text+0x50d): undefined reference to 
'HashTab::addel(std::string, std::string, std::string, std::string, std::string)' 


Here is 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
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include "HashTab.h"

using namespace std;

void runPrompt();
void add(string f, string l, string id, string c, string m);
void rem(string cid);
void print();
void error(string s);

HashTab * hashtab = NULL;

//this is where i will store the string tokens
string strarray[10];

int main()
{
	runPrompt();

}

void runPrompt()
{
	string input;
	int index;

	while(input != "quit")
	{
	    index = 0;
		cout << "set> ";
		//get the input and split into iss stream
		getline(cin,input);
		istringstream iss(input);

		while(iss)
		{
			//gather strings delimited by ' ' and store in array
			//index starts at 0, so increment after
			iss >> strarray[index];
			index++;
		}

		if(input.find("add") != string::npos)
		{

			//add customer id, first name, last name
			//strarray[0] = 'add'
			//[1] = fname, [2] = lname, [3] = id
			//[4] = classyear, [5] = major
			add(strarray[1], strarray[2], strarray[3],
			strarray[4], strarray[5]);
		}

		if(input.find("remove") != string::npos)
		{
			//remove
			//strarray[0] = remove
			//[1] = id
			rem(strarray[1]);
		}


		if(input.find("print") != string::npos)
		{
			print();
		}

		if(input.find("quit") != string::npos)
		{
		    //free up space
			break;
		}

		if(input.find("quit") == string::npos && input.find("print") == string::npos &&
                    input.find("remove") == string::npos && 
                    input.find("add") == string::npos)
		{
			error(input);
		}
	}
}



void print()
{

}

void add(string f, string l, string id, string c, string m)
{
	
    hashtab->addel(f, l, id, c, m);
}

void rem(string num)
{
	
}

void error(string input)
{
		cout << "Error!  Command not found: " << input <<endl;
}


HashTab.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef HASHTAB_H
#define HASHTAB_H
#include "Element.h"
#include <string.h>

using namespace std;

class HashTab
{
    public:
        HashTab();
        void addel(string fname, string lname, string id, string year, string major);
    private:
        Element elarray[10];
};

#endif // HASHTAB_H 


HashTab.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
#include <stdio.h>
#include <string.h>
#include "Element.h"
#include "HashTab.h"

using namespace std;

HashTab::HashTab()
{

}

void HashTab::addel(string fname, string lname, string id, string year, string major)
{
    //convert id to int
    int sid = atoi(id);

    //hash according to student id
    int key = sid % 10;

    //-1 means spot hasnt been used
    if(elarray[key]->getKey() == -1)
    {
        //set data and store key in element
        elarray[key]->setData(fname, lname, sid, year, major);
        elarray[key]->setKey(key);
    }

    else
    {
        for( int j = 0; j < 10; j++)
        {
            key = (key + j^2) % 10;
        }

        if(elarray[key]->getKey() == -1)
        {
            elarray[key]->setData(fname, lname, sid, year, major);
            elarray[key]->setKey(key);
            break;
        }
    }

}


I don't think the error is in my Element or Student class, but if it is requested then I will post them as well. Thanks!

EDIT: Formatted a little better to not make the page super wide. Sorry.
Last edited on
Looks like a typo. You've defined a method HashTab::addel but your error message indicates that you're attempting to call HashTab::addelm.
Last edited on
Thats a typo on my fault copying the error message into the topic. It actually says HashTab::addel in the error message.

I'll edit the OP to fix it. Sorry.
Then I'd guess you're somehow not including HashTab.o in the list of files you're linking together.

I'll also note that, once you get it to link, it will crash. You initialise hashtab to NULL at line 15 of main.cpp, and nowhere do you ever assign it to point to an actual instance of HashTab.
How would I go about linking it? I'm using an IDE and I guess it isn't linking it automatically, so what command would I use?

The NULL thing was a good catch, thanks.
Last edited on
There's no way to be sure, since you haven't told us what IDE you're using. But presumably you need to make sure HashTab.cpp is part of your project.

When you build your project, does it compile HashTab.cpp into an object file? Or just main.cpp?
I finally got it to work, I had to make a new project in Code::Blocks and it linked everything fine and compiled, but now it just shows a black screen with a blinking cursor. I am going to make another thread for that since the topic of this thread has been solved. Thank you MikeyBoy.
You're welcome! Glad it worked :)
Topic archived. No new replies allowed.