![](/img/trans.png)
[英]invalid initialization of non-const reference of type ‘int&' from a temporary of type 'MyClass<int>::iterator*'
[英]“invalid cast from type 'const myClass' to type 'int'”, how can I make it valid?
我从库头的以下摘录中的最终函数中收到关于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;
}
};
错误是:
从'const myClass'类型转换为'int'类型无效
有什么办法解决这个问题? 头文件名为FBaseColHashMapT.h
我已经添加了operator>
和operator<
方法,但我不知道如何让我的类进行哈希或如何允许上面需要的强制转换,而不是继承它,但我想看看我是否可以避免这种情况 为了支持我写的这两个运营商:
inline int GetHashCode() const {return myIntMember/4 + clientRect.GetHashCode();}
也许它可以在这里再次使用?
我提供myClass
作为此模板类的键, int
作为值。
显然,类期望KeyType
(在您的情况下是myDescriptor
)可以转换为int
。 因此,修复方法是将该转换添加到myDescriptor
:
class myDescriptor
{
public:
operator int() const { return (whatever the library expects, probably a hash key); }
// ...
};
您的类需要提供到int的转换:
myClass::operator int() const {
return myIntMember/4 + clientRect.GetHashCode();
}
...或者您需要专门化hashmap类的GetHashCode()
成员函数(您可以在myClass的头文件中执行此操作,但在hashmap的命名空间中):
template<class KeyType>
inline int __HashMapDefaultProviderT::GetHashCode(const myClass& obj) const {
return obj.myIntMember/4 + obj.clientRect.GetHashCode();
}
我不熟悉bada,所以我不能说哪个是预期的方法。 然而,两者都是一种循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.