本文介绍了如何将构造函数作为键存储在stdext :: hash_map中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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;
}


编译完上面的代码后,我得到以下错误消息
错误C2440:类型转换":无法从"const MyStruct"转换为"size_t"


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?

推荐答案


operator size_t() const
{
    return sizeof( REV_DCM_CACHE_KEY );
}


这篇关于如何将构造函数作为键存储在stdext :: hash_map中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 15:23