I copied this into my hash table header file so I can use a different string hash function, but it causes a redefinition error. My teacher said I could just put the code below in the header file, but it obviously doesn't work since it causes a redefinition of the default string hash function. What do I have to change for the code below to override the default string hash function?
You can't specialize std::hash<std::string> because it has already been specialized. If you want to create your own hash function you should use a different name.
The problem with using a different name is that your table's hash method is no longer general, and the hash method now needs to identify when a key is a string and when it is not. Which really creates a lot of work to recode everything. Is there another method?
The problem with using a different name is that your table's hash method is no longer general...
The problem with the general method is that you don't want the default behavior, so specificity is in order.
...and the hash method now needs to identify when a key is a string and when it is not.
The compiler already needed to identify the type of the key to generate the hash function. You're not introducing any inefficiency in the form of extra overhead here.
Which really creates a lot of work to recode everything.
Not really.
Is there another method?
You could create your own type with a specialized hash function and store that type in lieu of a string. Of course, that really does create more work for you.