0

Here's the prototype of the function:

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

in the function, I tried to call:

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

and here's getLeftChild:

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

Then, it reports an error:

no instance of overloaded function "BinaryDimonTree::insert" matches the argument list -- argument types are: (int, int, int, TreeNode *)

If I change my code this way:

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

No error would be reported.

I wonder why this problem occurs and how to solve it if I want to directly use the function return value as the parameter?

Thanks a lot!

Sean
  • 1,055
  • 11
  • 10

1 Answers1

2

The last argument of your insert function is non-const reference to a pointer. In the call:

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

the last argument is a temporary value of TreeNode * type, that cannot bind to the TreeNode * &. You can find a good explanation here

Dmitry Gordon
  • 2,229
  • 12
  • 20