title: invalid use of non-static data member in bst
body: <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  class Node{
  private:
      int data;
      Node *left;
      Node *right;
  public:
      Node(int data){                  //  node():data(data) ,left(nullptr), right(nullptr) {};
          this-&gt;data=data;
          this-&gt;left=nullptr;
          this-&gt;right=nullptr;
      }
      int get_data(){ return data;}
      friend class BST;
  };
  
  class BST{
  private:                                 // just bcoz BST is a friend u can't access it's data, u need to create a object for that.
      Node *root;
  public:
      BST(){ root=nullptr;}
      Node* get_root(){ return root;}              //  getting data using friend class -&gt; int get_data(Node*tree){ return tree-&gt;data;}   and do bst.get_dat(tail) in cases.
      void insert(int data,Node*tree);
      void remove(int data);
      Node* search_item(int data,Node*tree);
      Node* find_min(Node*tree);
      Node* find_max(Node*tree);
      /*void preorder(node *);
      void inorder(node *);
      void postorder(node *);
      void display(node *, int);*/
  };
  
  void BST::insert(int data,Node*tree=root){
      Node *temp= new Node(data);
      if(root==nullptr){
          root=temp;
          return;
      }
      else if(tree-&gt;data==data)
          return;                              // since duplicates not allowed we don't insert them.
      else if(data &lt; tree-&gt;data){
          if(tree-&gt;left!=nullptr)
              return insert(data,tree-&gt;left);
          else{
              tree-&gt;left=temp;
              return;
          }
      }
      else if(data &gt; tree-&gt;data){
          if(tree-&gt;right!=nullptr)
              return insert(data,tree-&gt;right);
          else{
              tree-&gt;right=temp;
              return;
          }
      }
  }
  /*void BST::remove(int data){
      Node *tree=search_item(Node*tree=root,int data);
  }*/
  Node* BST::search_item(int data,Node*tree=root){
      if(root==nullptr){
          cout&lt;&lt;"Value not found"&lt;&lt;endl;
          return root;
      }
      else if(root-&gt;data==data){
          cout&lt;&lt;"Value found"&lt;&lt;endl;
          return root;
      }
      else if(tree-&gt;data==data){
          cout&lt;&lt;"Value found"&lt;&lt;endl;
          return tree;
      }
      else if(data &lt; tree-&gt;data){
          if(tree-&gt;left!=nullptr)
              return search_item(data,tree-&gt;left);
          else{
              cout&lt;&lt;"Value not found"&lt;&lt;endl;
              return nullptr;
          }
      }
      else if(data &gt; tree-&gt;data){
          if(tree-&gt;right!=nullptr)
              return search_item(data,tree-&gt;right);
          else{
              cout&lt;&lt;"Value not found"&lt;&lt;endl;
              return nullptr;
          }
      }
  }
  Node* BST::find_min(Node*tree){
      if(root==nullptr || root-&gt;left==nullptr)
          return root;
      else if(tree-&gt;left==nullptr)
          return tree;
      else
          return find_min(tree-&gt;left);
  }
  Node* BST::find_max(Node*tree){
      if(root==nullptr || root-&gt;right==nullptr)
          return root;
      else if(tree-&gt;right==nullptr)
          return tree;
      else
          return find_max(tree-&gt;right);
  }
  int main(){
      BST bst;
      int i,data,f=0;
      Node *root,*tail;
      cout&lt;&lt;"Enter your choice:"&lt;&lt;endl;
      cout&lt;&lt;"1)Insert an item\n2)Delete an item\n3)Search_item\n4)Find minimum\n5)Find max\n6)Preorder Traversal\n7)Inorder Traversal\n9)Postorder Traversal\n7)Display\n"&lt;&lt;endl;
      while(1){
          cin&gt;&gt;i;
          switch(i){
              case 1:
                  cin&gt;&gt;data;
                  root=bst.get_root();
                  bst.insert(data);
                  break;
             /* case 2:
                  bst.remove();
                  break;*/
              case 3:
                  root=bst.get_root();
                  cin&gt;&gt;data;
                  tail=bst.search_item(data);
                  break;
              case 4:
                  root=bst.get_root();
                  tail=bst.find_min(root);
                  cout&lt;&lt;tail-&gt;get_data()&lt;&lt;endl;
                  break;
              case 5:
                  root=bst.get_root();
                  tail=bst.find_max(root);
                  cout&lt;&lt;(*tail).get_data()&lt;&lt;endl;
                  break;
              /*case 4:
                  bst.preorder();
                  break;
              case 5:
                  bst.inorder();
                  break;
              case 6:
                  bst.postorder();
                  break;
              case 7:
                  bst.display();
                  break;*/
              default:
                  f=1;
          }
          if(f==1)
              break;
      }
      return 0;
  }
  </code></pre>
  
  <blockquote>
    <p>invalid use of non-static data member 'BST::root'</p>
  </blockquote>
  
  <p>I've been trying to implement bst using 2 classes. Can you guys help me understand why this error is coming and please suggest the required correction?</p>
  
  <p>This error is coming in line no. 36 and 64 which are these lines:</p>
  
  <pre><code>void BST::insert(int data,Node*tree=root)         /* line 36 */
  
  Node* BST::search_item(int data,Node*tree=root)   /* line 64 */
  </code></pre>