Click here to Skip to main content
15,887,413 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
#include "hash_map"

struct MyStruct
{
    MyStruct( const void* pInfo, UINT uNumber ) : m_pInfo( pInfo ),
                                                  m_uNumber( uNumber )
    {
    }
    friend bool operator< ( const MyStruct& stKey1_i, const MyStruct& stKey2_i )
    {
        if( stKey1_i.m_pInfo == stKey2_i.m_pInfo )
        {
            return stKey1_i.m_uNumber < stKey2_i.m_uNumber;
        }
        else
        {
            return stKey1_i.m_pInfo < stKey2_i.m_pInfo;
        }
    }
    const void* m_pInfo;
    UINT m_uNumber;
};

int _tmain(int argc, _TCHAR* argv[])
{
    typedef stdext::hash_map< const MyStruct, int > MyMap;
    MyMap map;
    MyStruct mystructObj1 = MyStruct( new char[20], 10 );
    map[ mystructObj1 ] = 500;
    return 0;
}

After compiling the above code i got following error message
error C2440: 'type cast' : cannot convert from 'const MyStruct' to 'size_t'

How to solve this?
Posted

Well, here[^] you will find a good explanation on how to use hash_map with your custom classes/structures. :)
 
Share this answer
 
add following method into MyStruct
operator size_t() const
{
    return sizeof( REV_DCM_CACHE_KEY );
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900