【发布时间】:2016-04-20 08:22:22
【问题描述】:
ValueType& operator[](const KeyType& key) {
Node<Pair> *cur_node = this->_head;
while (cur_node != nullptr) {
if (cur_node->Data().first == key) {
//ValueType& ref = cur_node->Data().second;
return cur_node->Data().second;
}
cur_node = cur_node->Next();
int a = 5;
int& ra = a;
}
您好,ValueType 被定义为实现此运算符的模板的类。就我而言,它是int。 “第二个”也来自 ValueType 类型(不是参考)。我确实需要从函数返回对 ValueType 类对象的引用;
错误:
return: cannot cast from int to int&
尽管带有 a 和 ra 的简单示例确实符合要求。初始化 ref 并像在评论中一样返回它也会给出同样的错误,我该如何处理它? 谢谢。
【问题讨论】:
标签: c++ casting reference operator-overloading pass-by-reference