I'm just starting to work on a project for class and I'm getting an error that I'm unsure how to fix. I'll do my best to provide all necessary details but if you need any more info please let me know. The project is to create a simple hash function and I'm getting a linker error when trying to compile. I will post all of my code as well as the error I receive when compiling. I have taken two previous programming classes but that was quite a while ago and I'm pretty out of practice at the moment so it may be an obvious mistake. Ignore all commented out code in hash.h because that is just function definitions that my teacher provided that I have not yet implemented. She also specifically requested that we put our hashing function in its own separate file. I should also specify that I'm using a Windows machine with cygwin64 to compile my code.
hash.h:
#ifndef __HASH_H
#define __HASH_H
#include <string>
#include <list>
using std::string;
using std::list;
class Hash {
public:
void remove(string); // remove key from hash table
//void print(); // print the entire hash table
void processFile(string); // open file and add keys to hash table
//bool search(string); // search for a key in the hash table
//void output(string); // print entire hash table to a file
//void printStats(); // print statistics
private:
// HASH_TABLE_SIZE should be defined using the -D option for g++
//list<string> hashTable [HASH_TABLE_SIZE];
list<string> hashTable [100];
int collisions;
int longestList;
double avgLength;
int hf(string); // the hash function
void insert(string);
// put additional variables/functions below
// do not change anything above!
};
#endif
hash.cpp:
#include "hash.h"
void Hash::remove(string string_to_remove)
{
int index = hf(string_to_remove);
for(list<string>::iterator iter = hashTable[index].begin(); iter != hashTable[index].end(); iter++)
{
if(*iter == string_to_remove)
hashTable[index].erase(iter);
}
}
void Hash::insert(string string_to_add)
{
int index = hf(string_to_add);
hashTable[index].push_back(string_to_add);
}
void Hash::processFile(string file_name)
{
insert(file_name);
}
hash_function.cpp
#include "hash.h"
int Hash::hf(string input)
{
int sum = 0;
for (int i = 0; i < 5; i++)
sum += (int)input[i];
cout << sum % 100 << endl;
return [sum % 100 /*HASH_TABLE_SIZE*/];
}
Compiler Error:
$ g++ hash_function.cpp testmain.cpp hash.cpp -o test
/tmp/cc42z5XM.o:hash.cpp:(.text+0x32): undefined reference to `Hash::hf(std::string)'
/tmp/cc42z5XM.o:hash.cpp:(.text+0x32): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Hash::hf(std::string)'
/tmp/cc42z5XM.o:hash.cpp:(.text+0x13c): undefined reference to `Hash::hf(std::string)'
/tmp/cc42z5XM.o:hash.cpp:(.text+0x13c): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Hash::hf(std::string)'
collect2: error: ld returned 1 exit status