0

I'm trying to write the implement of BST in OOP style.I got some bugs while writting insertNode function like

error: invalid use of non-static data member ‘BST::root’

.How can i fix it?

struct node{
    int data;
    node* left;
    node* right;
    node(){}
    node(int data){
        this->data=data;
        this->left=NULL;
        this->right=NULL;
    }
};
struct BST{
    node* root;
public:
    BST(){root=NULL;}
    node*& getRoot(){return root;}
    void insert(int data,node*& t=this->root){
        if(!t){
            t=new node(data);
            return;
        }
        if(t->data<data) insert(data,t->left);
        else insert(data,t->right);
    }
};
Jason
  • 36,170
  • 5
  • 26
  • 60
Bach
  • 1
  • You cant do this. – NathanOliver Jul 20 '22 at 03:43
  • 1
    Sure you can... Just overload insert (two insert functions) to take one arg and call the other form with root as second arg. Also if you getRoot, you might as well make root public. – Abel Jul 20 '22 at 03:45

0 Answers0