【发布时间】:2016-11-01 09:39:22
【问题描述】:
template <class T>
bool BST<T>::search(const T& x, int& len) const
{
return search(BT<T>::root, x);
}
template <class T>
bool BST<T>::search(struct Node<T>*& root, const T& x)
{
if (root == NULL)
return false;
else
{
if (root->data == x)
return true;
else if(root->data < x)
search(root->left, x);
else
search(root->right, x);
}
}
这是我的搜索功能,用于搜索带有 T 节点的 BST 类。 x 是在树中搜索的数据,len 只是它必须经过的节点数量才能找到匹配节点(如果存在)。我还没有实现这一点,我只是在逐步发展我的任务。我通过这样做来调用它:
if(t.search(v[1], len) == true)
cout << endl << "true";
v 只是我必须创建的一个向量来比较它,所以这只是为它提供一个 int。我得到的错误:
BST.h: In member function âbool BST<T>::search(const T&, int&) const [with T = int]â:
prog5.cc:24: instantiated from here
BST.h:78: error: no matching function for call to âBST<int>::search(Node<int>* const&, const int&) constâ
BST.h:76: note: candidates are: bool BST<T>::search(const T&, int&) const [with T = int]
BST.h:83: note: bool BST<T>::search(Node<T>*&, const T&) [with T = int]
所以我不确定自己做错了什么或哪里做错了。
【问题讨论】:
-
大量信息丢失。什么是 BT
::root? BST和BT是一回事吗?节点和节点是一回事吗?请剪切粘贴,请勿输入。 -
好吧,这是派生函数的一部分,所以要传递根节点,我必须专门告诉它 BT
::root 否则我会出错。 -
如果我不这样做,我得到的 root 没有在这个范围内声明。