'invalid use of non-static data member in bst
#include<iostream>
using namespace std;
class Node{
private:
int data;
Node *left;
Node *right;
public:
Node(int data){ // node():data(data) ,left(nullptr), right(nullptr) {};
this->data=data;
this->left=nullptr;
this->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 -> int get_data(Node*tree){ return tree->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->data==data)
return; // since duplicates not allowed we don't insert them.
else if(data < tree->data){
if(tree->left!=nullptr)
return insert(data,tree->left);
else{
tree->left=temp;
return;
}
}
else if(data > tree->data){
if(tree->right!=nullptr)
return insert(data,tree->right);
else{
tree->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<<"Value not found"<<endl;
return root;
}
else if(root->data==data){
cout<<"Value found"<<endl;
return root;
}
else if(tree->data==data){
cout<<"Value found"<<endl;
return tree;
}
else if(data < tree->data){
if(tree->left!=nullptr)
return search_item(data,tree->left);
else{
cout<<"Value not found"<<endl;
return nullptr;
}
}
else if(data > tree->data){
if(tree->right!=nullptr)
return search_item(data,tree->right);
else{
cout<<"Value not found"<<endl;
return nullptr;
}
}
}
Node* BST::find_min(Node*tree){
if(root==nullptr || root->left==nullptr)
return root;
else if(tree->left==nullptr)
return tree;
else
return find_min(tree->left);
}
Node* BST::find_max(Node*tree){
if(root==nullptr || root->right==nullptr)
return root;
else if(tree->right==nullptr)
return tree;
else
return find_max(tree->right);
}
int main(){
BST bst;
int i,data,f=0;
Node *root,*tail;
cout<<"Enter your choice:"<<endl;
cout<<"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"<<endl;
while(1){
cin>>i;
switch(i){
case 1:
cin>>data;
root=bst.get_root();
bst.insert(data);
break;
/* case 2:
bst.remove();
break;*/
case 3:
root=bst.get_root();
cin>>data;
tail=bst.search_item(data);
break;
case 4:
root=bst.get_root();
tail=bst.find_min(root);
cout<<tail->get_data()<<endl;
break;
case 5:
root=bst.get_root();
tail=bst.find_max(root);
cout<<(*tail).get_data()<<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;
}
invalid use of non-static data member 'BST::root'
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?
This error is coming in line no. 36 and 64 which are these lines:
void BST::insert(int data,Node*tree=root) /* line 36 */
Node* BST::search_item(int data,Node*tree=root) /* line 64 */
Solution 1:[1]
Write this before the class as:
class Node;
Node *root;
and make the root point at the root in the function that insert nodes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user438383 |
