C++ C2440 Can';t将int转换为int&;
我的代码有点问题。我想我错过了一些指针,但找不到 这是我的课程代码:C++ C2440 Can';t将int转换为int&;,c++,C++,我的代码有点问题。我想我错过了一些指针,但找不到 这是我的课程代码: template <class T, class U> class KeyValue { private: T _key; U _value; public: KeyValue(); KeyValue(T key, U value) { this->_key = key; this->_value = value };
template <class T, class U>
class KeyValue
{
private:
T _key;
U _value;
public:
KeyValue();
KeyValue(T key, U value)
{
this->_key = key;
this->_value = value
};
T GetKey() { return this->_key; }
U GetValue() { return this->_value; }
};
模板
类键值
{
私人:
T_键;
U_值;
公众:
KeyValue();
键值(T键,U值)
{
此->\u键=键;
此->\u值=值
};
T GetKey(){返回此->\u key;}
U GetValue(){返回此->\U value;}
};
错误发生在:
template<class T, class U>
inline U & SparseArray<T, U>::operator[](T key)
{
for (std::list<KeyValue<T, U>>::iterator it = list->begin(); it != list->end(); it++)
{
if (it->GetKey() == key)
{
return it->GetValue();
}
}
return (list->insert(list->begin(), KeyValue<T, U>(key, U())))->GetValue();
}
模板
内联U&SparseArray::运算符[](T键)
{
对于(std::list::iterator it=list->begin();it!=list->end();it++)
{
if(it->GetKey()==key)
{
返回它->GetValue();
}
}
return(list->insert(list->begin(),KeyValue(key,U()))->GetValue();
}
GetValue()
按值返回,这意味着它将为您提供一个prvalue。prvalue是一个临时对象,在完整表达式末尾超出范围。因此,不允许将左值引用绑定到它,这就是您的返回类型(U&
)
如果要返回对基础\u键的引用,则GetValue()
需要返回左值引用,如
GetValue()
按值返回,这意味着它将为您提供一个prvalue。prvalue是一个临时对象,在完整表达式末尾超出范围。因此,不允许将左值引用绑定到它,这就是您的返回类型(U&
)
如果要返回对基础\u键的引用,则GetValue()
需要返回左值引用,如
请提供一份合适的表格。您在这里所描述的内容不足以让任何用户重新创建您的问题并对其进行诊断。inline U&
是您的成员声称返回的内容,但是obj->GetValue()
是它实际返回的内容,在实际实现中是U GetValue()
。右旋值。U
不是U&
。请提供正确的密码。您在这里所描述的内容不足以让任何用户重新创建您的问题并对其进行诊断。inline U&
是您的成员声称返回的内容,但是obj->GetValue()
是它实际返回的内容,在实际实现中是U GetValue()
。右旋值。U
不是U&
。
T& GetKey() { return this->_key; }