[英]Binary Tree Inorder traversal error: no matching function for call
我正在嘗試構建二進制樹,但我一直收到錯誤。 當我在main()
調用我的Inorder()
函數時,我得到錯誤:
錯誤:沒有用於調用'BinaryTree :: Inorder()'的匹配函數。
我希望有人可以幫我解決這個問題。
#include <iostream>
using namespace std;
class BinaryTree{
private:
struct TreeNode{
TreeNode *left;
TreeNode *right;
int data;
};
TreeNode *root;
public:
BinaryTree(){
root = NULL;
}
void Inorder(TreeNode *p){
if(p != NULL){
Inorder(p -> left);
cout<< p -> data;
Inorder(p -> right);
}
}
void InsertData(int data){
TreeNode *t = new TreeNode;
TreeNode *parent;
t -> data = data;
t -> left = NULL;
t -> right = NULL;
parent = NULL;
//is this a new tree?
if (isEmpty())
root = t;
else{
TreeNode *curr;
curr = root;
while(curr){
parent = curr;
if (t -> data > curr -> data)
curr = curr -> right;
else
curr = curr -> left;
}
if(t -> data < parent -> data)
parent -> left = t;
else
parent -> right =t;
}
}
bool isEmpty(){
return root == NULL;
}
};
int main(){
BinaryTree BT;
int num;
while (cin >> num)
BT.InsertData(num);
cout << "Inorder: " << BT.Inorder() << endl;
return 0;
}
Inorder
聲明如下:
void Inorder(TreeNode *p)
它需要一個TreeNode *
參數。 也許你打算通過BT.Inorder( BT.root )
?
好吧,你的void Inorder(TreeNode *p)
接受一個參數,而你的函數調用cout << "Inorder: " << BT.Inorder() << endl;
沒有。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.