Reputation: 2920
I have the following:
#include<iostream>
#include<unordered_map>
#include<tuple>
using namespace std;
class CTest {
// Properties
public:
unordered_map<const string, tuple<int, int> > Layout;
// Methods
public:
CTest ();
~CTest ();
};
CTest::CTest () {
Layout["XYZ"] = make_tuple (0, 1);
}
CTest::~CTest () {
// Do nothing
}
int main (int argc, char *argv[]) {
CTest Test;
return 0;
}
Compiling this simple programme gives the following error:
error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)
I'm using Visual Studio 2010 Professional in Windows 7.
Upvotes: 5
Views: 3359
Reputation: 27048
You need to remove the const qualifier on the key.
unordered_map<const string, tuple<int, int> > Layout;
into
unordered_map<string, tuple<int, int> > Layout;
this is because keys are always const, according to this answer:
Using a const key for unordered_map
I figure the underlying reason is related to Duplicate const qualifier allowed in C but not in C++?
Also, as other posts pointed out you may need to include string (although with gcc it seems to come with iostream)
Upvotes: 2
Reputation: 103741
As was pointed out, the reason for your initial error was that you needed to include <string>
. However, you may have another problem with this:
unordered_map<const string, tuple<int, int> > Layout;
You (may) need to remove the const from that string:
unordered_map<string, tuple<int, int> > Layout;
This may not be necessary on your compiler, but it is on mine. First of all, the const is superfluous, map/unordered_map keys are const anyway, but that is not the problem. The problem has to do with the hash function template not working for const types.
The following simple program isolates the problem for me:
#include <functional>
int main (int argc, char *argv[])
{
std::hash<const int> h;
h(10);
}
undefined reference to `std::hash<int const>::operator()(int) const'
I cannot, at present time, explain this.
Upvotes: 1
Reputation: 16588
Visual Studio 2010 will compile your source code as is as long as you #include <string>
so that it has the comparison tests for string
available.
As the other posters mention, you should also make your key a regular string
rather than a const string
, as this is conformant to the STL standard, but this is not strictly necessary to make VS 2010 compile the source above.
Upvotes: 1
Reputation: 122001
In addition to changing Layout
to:
unordered_map<string, tuple<int, int> > Layout;
as stated by Johan and Benjamin you also need to #include <string>
.
Note, I do not understand why the change to Layout
is required, even if the const
is superfluous.
Upvotes: 3