"从'const myClass'类型无效转换为'int'类型",我怎样才能使它有效?

Joh*_*ohn 2 c++ eclipse casting typecast-operator

我从库头的以下摘录中的最终函数中收到关于return语句(或强制转换)的错误

///////////////////////////////////////////////////////////
// class __HashMapDefaultProviderT

/**
* @internal
* @class    __HashMapDefaultProviderT
* @brief    This is an implementation of the IHashCodeProviderT interface for the HashMap class.
* @since    1.0
*/
template<class KeyType>
class __HashMapDefaultProviderT
    :   public IHashCodeProviderT<KeyType>,
    public Object
{
public:

    // Lifecycle

    /**
    * This is the default constructor for this class.
    *
    * @since     1.0
    */
    __HashMapDefaultProviderT(void) {}


    /**
    * This is the destructor for this class.
    *
    * @since     1.0
    */
    virtual ~__HashMapDefaultProviderT(void) {}


    // Operation

    /**
    * Gets the hash code of the specified object
    *
    * @since        1.0
    * @return       The hash code of the specified object
    * @see          Osp::Base::Object::GetHashCode
    */
    int GetHashCode(const KeyType& obj) const
    {
        return (int)obj;
    }


};
Run Code Online (Sandbox Code Playgroud)

错误是:

从'const myClass'类型转换为'int'类型无效

有什么办法解决这个问题?头文件名为FBaseColHashMapT.h

我添加operator>operator<已方法,但我不知道是怎么回事,让我的类散列或如何以允许上面需要投,短继承它,但我想看看我是否能避免这种情况.为了支持我写的这两个运营商:

inline int GetHashCode() const {return myIntMember/4 + clientRect.GetHashCode();}
Run Code Online (Sandbox Code Playgroud)

也许它可以在这里再次使用?

我提供myClass作为此模板类的键和int值.

cel*_*chk 8

显然,班级期望KeyType(在你的情况下myDescriptor)可以转换为int.因此,修复方法是将该转换添加到myDescriptor:

class myDescriptor
{
public:
  operator int() const { return (whatever the library expects, probably a hash key); }
  // ...
};
Run Code Online (Sandbox Code Playgroud)