0

For some odd reason, when I try to loop through my vector of strings

std::vector<Function> functionList;
std::vector<std::string> functionNames;

and try to store them in my hash_map

std::hash_map<Function, std::string> functions;

I come across 10 error messages all telling me that I have incorrectly used the binary operator '<'. I'm looping through like this:

for (auto i = 0; i < buffer.functionSize; i++)
    {
        functions[buffer.functionList[i]] = buffer.functionNames[i];
    }
}

And the buffer object is another class in which the std::vectors are stored. The exact error messages are posted below. Any ideas?

Error   1   error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const`std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc>`&' from 'const Function'

EDIT: I changed the hash_maps to unordered_maps and I now get the error:

Error   1   error C2338: The C++ Standard doesn't provide a hash for this type.
devCorner
  • 193
  • 4
  • 15
  • 1
    What type is `Function`? Which line(s) above elicit the error? – Brian Cain Jan 04 '15 at 02:13
  • Can you fix the error message? It looks incomplete right now. Also `std::hash_map` is some non-standard extension, you should use [`std::unordered_map`](http://en.cppreference.com/w/cpp/container/unordered_map) instead. – Praetorian Jan 04 '15 at 02:15
  • Function is a class that I made, and the error is in the assignment of the hash_map on the inside of the loop. – devCorner Jan 04 '15 at 02:15
  • Is your `Function` class defined (not just declared) before the line(s) with the error? – Brian Cain Jan 04 '15 at 02:16
  • It is defined. I know this because I can loop through the vectors and get the proper results. The error doesn't show up until I try to store the data in the hash_map – devCorner Jan 04 '15 at 02:18
  • 4
    The [MSDN documentation for `hash_map`](http://msdn.microsoft.com/en-us/library/0d462wfh.aspx) says it uses `operator<` within the default `Traits` template argument (`hash_compare >`). My guess is you haven't implemented `operator<` for your `Function` class. – Praetorian Jan 04 '15 at 02:21
  • And to fix your latest error after changing to `unordered_map`, add a specialization for `std::hash` for `Function`, and learn to read documentation! – Praetorian Jan 04 '15 at 02:22
  • And before the inevitable next question, `unordered_map` also requires you to implement `operator==` for the Key type. It uses that in cases where there is a hash collision. – Praetorian Jan 04 '15 at 02:24

0 Answers0