On 25 Jul 2005 16:53:04 -0700, Tommo wrote:
Ok, I am having some problems trying to store my KeyType as a pointer.
Error message below ( may not be that helpful )
Error: Could not find a match for std::map<MarketKey*, market_item_t*,
std::less<MarketKey*>, std::allocator<std::pair<MarketKey*const,
market_item_t*>>>::find(const MarketKey*).
It seems the find method is failing.
The find method is :
MktIter it = marketsMap->find(key);
where MktIter is : map < MarketKey*, market_item_t* >::iterator
MktIter;
my map is defined as : map < MarketKey*, market_item_t* > *marketsMap;
My MarketKey is:
class MarketKey : public BaseKey
{
public:
MarketKey(){};
virtual ~MarketKey(){};
bool operator<(const MarketKey &rhs) const;
void setMarket(uint8_t market);
uint8_t getMarket() const;
};
Now is there anything special i have to do with operator< now I am
wanting to store pointers as keys?? It works fine with 'by value' keys
but as soon as i try and use pointers i get compiliation errors
Your key type is a pointer, but your operator< is defined on references.
You need to define an operator< on pointers, but you can't write
operator<(T*,T*), because at least one parameter has to be of a
user-defined type, and all pointer types are considered built-in. So you'll
have to write a comparison class, e.g.
struct DerefLess
{
template<class T>
bool operator()(const T* x, const T* y) const
{
return *x < *y;
}
};
This class can take advantage of any type T that defines operator<, and you
could use it like this:
typedef map < MarketKey*, market_item_t*, DerefLess > MarketMap;
Of course, you can write it without using templates or depending on an
operator< defined on MarketKey objects:
struct DerefMarketKeyLess
{
bool operator()(const MarketKey* x, const MarketKey* y) const
{
return x->getMarket() < y->getMarket();
}
};
There's yet another variant that makes DerefLess a class template whose
operator() is just a normal member function, not a member template.
--
Doug Harrison
Microsoft MVP - Visual C++