0

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
SethMc
  • 119
  • 1
  • 10
  • 1
    `#define __HASH_H` triggers undefined behaviour since identifiers starting with `__` are reserved. –  Oct 17 '14 at 22:33
  • 3
    `return [sum % 100 /*HASH_TABLE_SIZE*/];` This can't possibly compile. – T.C. Oct 17 '14 at 22:34
  • @rightfold: I tried changing that to #define HASH_H and it made no difference, thank you for the answer though. – SethMc Oct 17 '14 at 23:26
  • @T.C.: Why exactly is that? All it is is return [sum % 100] with a comment following the 100. I'm not getting any kind of error in regards to that line of code like you would expect if the syntax there was incorrect. I tried changing that line and it made no difference, thank you for the help though. – SethMc Oct 17 '14 at 23:28
  • `[sum % 100]` simply isn't a valid expression. – T.C. Oct 17 '14 at 23:34
  • @T.C.: I'm pretty sure it is a valid expression as I have seen many other people do it. Just to check I changed my code in that portion to this: int x = sum % 100 return x; This made no difference either. – SethMc Oct 17 '14 at 23:41
  • I forgot the semicolon after 100 in the previous comment... – SethMc Oct 17 '14 at 23:49
  • Anyone have an answer for this? – SethMc Oct 18 '14 at 16:33

0 Answers0