【问题标题】:casting from variabble type to referencce to variable从变量类型转换为对变量的引用
【发布时间】: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


    【解决方案1】:

    好的,问题是 Data() 方法没有通过引用返回数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-03
      • 2011-09-13
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 2015-09-26
      相关资源
      最近更新 更多