I have a set of files that compile with a makefile to make a separate chaining hashing program. The program functions until I added code for insert, remove, and contains functions. I pulled code straight out of the book, but I'm getting an ambiguous error I cannot figure out and am hoping someone here can help identify it. I didn't post the whole program because I'm taking an educated guess that the cause of the error won't be found outside this code (but I could be wrong)
The error in question is:
Undefined first referenced
symbol in file
hash(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)hashApp.o
Also, not sure if this is relevant, but if I try and compile the .cpp file with the functions by itself I get:
Undefined first referenced
symbol in file
main /opt/csw/gcc3/lib/gcc/sparc-sun-solaris2.8/3.4.6/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
Here are the functions, strings are being hashed in a vector of lists:
template <class HashObj>
bool HashTable<HashObj>::contains(HashObj &item)
{
const list<HashObj> & whichList = theLists[ myhash( item ) ];
return find( whichList.begin( ), whichList.end( ), item ) != whichList.end( );
}
template <class HashObj>
bool HashTable<HashObj>::insert(const HashObj &item)
{
list<HashObj> & whichList = theLists[ myhash( item ) ];
if( find( whichList.begin( ), whichList.end( ), item ) != whichList.end( ) )
return false;
whichList.push_back( item );
return true;
}
template <class HashObj>
bool HashTable<HashObj>::remove(const HashObj &item)
{
list<HashObj> & whichList = theLists[ myhash( item ) ];
typename list<HashObj>::iterator itr = find( whichList.begin( ), whichList.end(), item );
if( itr == whichList.end( ) )
return false;
whichList.erase(itr);
return true;
}
This is the myhash function from the same file:
template <class HashObj>
int HashTable<HashObj>::myhash(const HashObj &item) const
{
int hashVal = hash(item);
hashVal %= theLists.size();
if (hashVal < 0)
hashVal += theLists.size();
return hashVal;
}
The above .cpp code has an include for hashTable.h, which in turn includes hashPrototypes.h
In hashPrototypes.h is
int hash(int key);
int hash(const string &key);
and my hash function gets compiled from a makefile that creates an executable based on what you enter. For example, I'm using hash1.cpp, so by typing make HASH=hash1, it should compile them all together.
Here is my hash1.cpp code:
#include "hashTable.h"
#include <cmath>
#include <cstdlib>
using namespace std;
template <class HashObj>
int hash(const HashObj &item)
{
int hashVal = 0;
for( int i = 0; i < item.length( ); i++ )
hashVal = 37 * hashVal + item[ i ];
return hashVal;
}
If you think the error is in the makefile, here is the makefile code:
# Make file for hashing
# Executable for the program will be in: hashTest
#default function is looked for in hashS1
#to give it another function make=filename without the suffix
HASH = hashS1
$(HASH)Test: $(HASH).o hashTable.o hashApp.o
g++ -o $(HASH)Test $(HASH).o hashTable.o hashApp.o
hashApp.o: hashTable.h hashPrototypes.h hashApp.cpp hashTable.cpp
g++ -c hashApp.cpp
hashTable.o: hashTable.h hashTable.cpp $(HASH).cpp
g++ -c hashTable.cpp
$(HASH).o: hashPrototypes.h $(HASH).cpp
g++ -c $(HASH).cpp
clean:
rm -f *.o
touch *