Reputation: 495
I am trying to build a Binary Tree, but I keep getting an error. When I call my Inorder()
function in main()
I get the error:
error: no matching function for call to 'BinaryTree :: Inorder()'.
I was hoping someone could help me figure this out.
#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;
}
Upvotes: 1
Views: 1078
Reputation:
Well, your void Inorder(TreeNode *p)
takes an argument, while your function call cout << "Inorder: " << BT.Inorder() << endl;
gives none.
Upvotes: 1
Reputation: 137770
Inorder
is declared like this:
void Inorder(TreeNode *p)
It needs a TreeNode *
argument. Perhaps you intended to pass BT.Inorder( BT.root )
?
Upvotes: 2