为什么在insert函数调用中不能直接使用返回指针作为参数?
Why cannot I directly use returned pointer as parameter in function call of insert?
函数原型如下:
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;
}
然后,报错:
no instance of overloaded function "BinaryDimonTree::insert" matches the argument list -- argument types are: (int, int, int, TreeNode *)
如果我这样更改我的代码:
TreeNode *child = cur->getLeftChild();
insert(x, y, ++level, child);
不会报错。
请问为什么会出现这个问题,想直接使用函数return值作为参数怎么解决?
非常感谢!
您的 insert
函数的最后一个参数是对指针的非常量引用。通话中:
insert(x, y, ++level, cur->getLeftChild());
最后一个参数是 TreeNode *
类型的临时值,不能绑定到 TreeNode * &
。你可以找到一个很好的解释here
函数原型如下:
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;
}
然后,报错:
no instance of overloaded function "BinaryDimonTree::insert" matches the argument list -- argument types are: (int, int, int, TreeNode *)
如果我这样更改我的代码:
TreeNode *child = cur->getLeftChild();
insert(x, y, ++level, child);
不会报错。
请问为什么会出现这个问题,想直接使用函数return值作为参数怎么解决?
非常感谢!
您的 insert
函数的最后一个参数是对指针的非常量引用。通话中:
insert(x, y, ++level, cur->getLeftChild());
最后一个参数是 TreeNode *
类型的临时值,不能绑定到 TreeNode * &
。你可以找到一个很好的解释here