【问题标题】:Why cannot I directly use returned pointer as parameter in function call of insert?为什么insert的函数调用中不能直接使用返回的指针作为参数?
【发布时间】:2019-09-05 10:24:58
【问题描述】:

这是函数的原型:

void BinaryDimonTree::insert(int x, int y, int level, TreeNode *&cur)

在函数中,我尝试调用:

insert(x, y, ++level, cur->getLeftChild());

这里是getLeftChild

TreeNode* TreeNode::getLeftChild() const {
    return this->left;
}

然后报错:

重载函数“BinaryDimonTree::insert”的实例与参数列表不匹配——参数类型为:(int, int, int, TreeNode *)

如果我这样更改代码:

TreeNode *child = cur->getLeftChild();
insert(x, y, ++level, child);

不会报告错误。

不知道为什么会出现这个问题,如果我想直接使用函数返回值作为参数怎么解决?

非常感谢!

【问题讨论】:

  • 您不能使用隐式 temp 作为参考参数。通话后您会如何提及它?

标签: c++ pointers data-structures function-parameter


【解决方案1】:

insert 函数的最后一个参数是对指针的非常量引用。在通话中:

insert(x, y, ++level, cur->getLeftChild());

最后一个参数是TreeNode * 类型的临时值,不能绑定到TreeNode * &。你可以找到一个很好的解释here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多