简体   繁体   中英

Binary Tree Inorder traversal error: no matching function for call

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;
}

Inorder is declared like this:

        void Inorder(TreeNode *p)

It needs a TreeNode * argument. Perhaps you intended to pass BT.Inorder( BT.root ) ?

Well, your void Inorder(TreeNode *p) takes an argument, while your function call cout << "Inorder: " << BT.Inorder() << endl; gives none.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM