Reputation: 42760
I have a class named error_code
. I use it as key for std::map
and CMap
(MFC). I am able to make it work for std::map
, but not CMap
. May I know how I can do so?
// OK!
std::map<error_code, int> m;
m[error_code(123)] = 888;
// error C2440: 'type cast' : cannot convert from 'error_code' to 'DWORD_PTR'
CMap <error_code, error_code&, int, int& > m;
m[error_code(123)] = 888;
class error_code {
public:
error_code() : hi(0), lo(0) {}
error_code(unsigned __int64 lo) : hi(0), lo(lo) {}
error_code(unsigned __int64 hi, unsigned __int64 lo) : hi(hi), lo(lo) {}
error_code& operator|=(const error_code &e) {
this->hi |= e.hi;
this->lo |= e.lo;
return *this;
}
error_code& operator&=(const error_code &e) {
this->hi &= e.hi;
this->lo &= e.lo;
return *this;
}
bool operator==(const error_code& e) const {
return hi == e.hi && lo == e.lo;
}
bool operator!=(const error_code& e) const {
return hi != e.hi || lo != e.lo;
}
bool operator<(const error_code& e) const {
if (hi == e.hi) {
return lo < e.lo;
}
return hi < e.hi;
}
unsigned __int64 hi;
unsigned __int64 lo;
};
Upvotes: 4
Views: 1942
Reputation: 3218
A quick trace shows that template function belows is causing the error :
template<class ARG_KEY>
AFX_INLINE UINT AFXAPI HashKey(ARG_KEY key)
{
// default identity hash - works for most primitive values
return (DWORD)(((DWORD_PTR)key)>>4);
}
A quick fix would involve adding implicit conversion function to the user-defined type. I'm not sure what data will be stored so just randomly pick some attribute to form the required data.
class error_code {
...
operator DWORD_PTR() const
{
return hi;
}
...
}
Upvotes: 2