C2440 無法將 int 轉換為 int &
C2440 Can't transform int to int &
我的代码遇到了一些麻烦。我想我错过了一些指针,但找不到它。
这是我的类代码:
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; }
};
错误发生在:
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();
}
GetValue()
按值返回,这意味着它为您提供了一个 prvalue。 PRvalue 是一个临时对象,在完整表达式的末尾超出范围。 因此,您不允许将左值引用绑定到它,而返回类型(U &
(就是这样。
如果要返回对基础_key
的引用,则GetValue()
需要返回一个左值引用,例如
T& GetKey() { return this->_key; }
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 我不断收到错误 C2440'=':无法从"int *(__cdecl *)(int *,int *,int,int)"转换为"int *
- C2440 無法將 int 轉換為 int &
- 错误 C2440:"=":无法从 'char *(__cdecl *)(int,int)' 转换为 en 'GetItemText_t'
- 返回 NULL,但收到错误 C2440:'return':无法从'int'转换为'const &'
- 错误 C2440:"返回":无法从"int [2]"转换为"int (&&)[2]"
- 错误 C2440:"=":无法从"int"转换为"int*"
- 如何修复此错误错误"error C2440: '=' : cannot convert from 'int (*)[]' to 'int *' "?
- 需要帮助。C++ Z:\动态分配.cpp(18):错误 C2440:'=':无法从 'int' 转换为 'int *'
- decltype 错误 C2440 无法从'int *'转换为'int *&'
- C2440 '=' 无法从 'int' 转换为 'BST:<int>:Node*
- 错误 c2440 '='无法从 int * 转换为类型<T> *
- 错误 C2440: '=':无法从 'int' 转换为 'char [5]'