I'm having trouble overloading the operator< for a class that I want to use as a key for an stl map.
It works fine for a comparison test, but when I try to use the std::map.find() function I get an error: binary '<' : no operator found which takes a left-hand operand of type 'const StrategyKey' (or there is no acceptable conversion)
I've posted my test code below. Note, it is the commented out line that causes the error.
class StrategyKey
{
public:
StrategyKey(){}
~StrategyKey(){}
StrategyKey(const char* a, const char* b):
str_A(a),
str_B(b)
{
}
bool operator< (const StrategyKey& stratKey)
{
if (stratKey.str_A < this->str_A)
{
return false;
}
else if (stratKey.str_A == this->str_A && stratKey.str_B < this->str_B)
{
return false;
}
else
{
return true;
}
}
bool operator== (const StrategyKey& StratKey)
{
if (StratKey.str_A == this->str_A && StratKey.str_B == this->str_B)
{
return true;
}
else
return false;
}
std::string str_A;
std::string str_B;
};
int _tmain(int argc, _TCHAR* argv[])
{
StrategyKey aKey("about", "apples");
StrategyKey bKey("before", "bananas");
map<StrategyKey, int> myMap;
//myMap.find(aKey);
if (aKey < bKey)
{
cout << "aKey < bKey" << endl;
}
else
cout << "aKey !< bKey" << endl;
cin.get();
return 0;
}