Sunday 15 May 2011

c++ - Undefined reference error when compiling with g++ compiler in cygwin -



c++ - Undefined reference error when compiling with g++ compiler in cygwin -

i'm starting work on project class , i'm getting error i'm unsure how fix. i'll best provide necessary details if need more info please allow me know. project create simple hash function , i'm getting linker error when trying compile. post of code error receive when compiling. have taken 2 previous programming classes quite while ago , i'm pretty out of practice @ moment may obvious mistake. ignore commented out code in hash.h because function definitions teacher provided have not yet implemented. requested set our hashing function in own separate file. should specify i'm using windows machine cygwin64 compile 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 hash table //void print(); // print entire hash table void processfile(string); // open file , add together keys hash table //bool search(string); // search key in hash table //void output(string); // print entire hash table file //void printstats(); // print statistics private: // hash_table_size should defined using -d alternative g++ //list<string> hashtable [hash_table_size]; list<string> hashtable [100]; int collisions; int longestlist; double avglength; int hf(string); // hash function void insert(string); // set additional variables/functions below // not alter 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; (int = 0; < 5; i++) sum += (int)input[i]; cout << sum % 100 << endl; homecoming [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 `hash::hf(std::string)' /tmp/cc42z5xm.o:hash.cpp:(.text+0x32): relocation truncated fit: r_x86_64_pc32 against undefined symbol `hash::hf(std::string)' /tmp/cc42z5xm.o:hash.cpp:(.text+0x13c): undefined reference `hash::hf(std::string)' /tmp/cc42z5xm.o:hash.cpp:(.text+0x13c): relocation truncated fit: r_x86_64_pc32 against undefined symbol `hash::hf(std::string)' collect2: error: ld returned 1 exit status

c++ reference linker undefined undefined-reference

No comments:

Post a Comment