类型的问题,关于引用demo代码如下:void BST::insert(BSTNode & n, BSTNode* & t){BSTNode* tempt-getLef
类型的问题,关于引用 demo代码如下: void BST::insert(BSTNode & n, BSTNode* & t) { BSTNode* temp=t->getLeft(); insert(n,temp); } 上述代码可以编译通过,但是改成这样: void BST::insert(BSTNode & n, BSTNode* & t) { insert(n,t->getLeft()); } 就报错: no matching function for call to ‘BST::insert(BSTNode&, BSTNode*) no known conversion for argument 2 from ‘BSTNode*’ to ‘BSTNode*&’ 请问这是为什么?上面两个代码应该是等效才对吧,不理解,多谢解释! [最优解释]
int main() { func(licheng()); // 错误 hanshu(licheng());
return 0; }
[其他解释]
明白这个为什么不行了,但是我的问题还是没法解决。这道题老师要求: Implement a tree class in whose code you should be sure to access tree nodes only through set and get method, make the child references to be private data members of the node class to insure that this happens. 后面还说必须用递归实现。 所以在BSTNode类里面我写一个get函数 const BSTNode* getLeft()const { return this->left; } 类里有private成员 BSTNode* left; 这样不行,因为insert函数中没法递归,怎么办呢?
In member function ‘BSTNode*& BSTNode::getLeft() const’: bstnode.h:55:17: error: invalid initialization of reference of type ‘BSTNode*&’ from expression