I have created a global object and assigned it to null. When i m trying to access the same object. It says it is ambigous. I cant able to find whats the problem with global declaration. Basically what i got ambigous happens when it is null. But even i cant able to do NULL check.
I have written the complete code. Check below code
using namespace std;
class node {
public :
int data;
class node *left,*right;
node(int data)
{
this->data=data;
this->left=NULL;
this->right=NULL;
}
};
class node* head=NULL;
class node* prev=NULL;
void bt2dll(class node *tree)
{
if(tree==NULL)
return ;
//static node *prev=NULL;
bt2dll(tree->left);
if(prev==NULL)
head=tree;
else
{
prev->right=tree;
tree->left=head;
}
prev=tree;
bt2dll(tree->right);
}
void print(node *root)
{
if (root != NULL)
{
print(root->left);
cout<<root->data<<" ";
print(root->right);
}
}
void printList(node *head)
{
while (head)
{
cout<<head->data<<" ";
head = head->right;
}
}
int main()
{
node *root = new node(10);
root->left = new node(12);
root->right = new node(15);
root->left->left = new node(25);
root->left->right = new node(30);
root->right->left = new node(36);
cout << "Inorder Trvaersal of given Tree is:\n";
print(root);
bt2dll(root);
cout << "\nDouble Linked list is:\n";
printList(head);
return 0;
}
prog.cpp:23:5: error: reference to ‘prev’ is ambiguous
if(prev==NULL)
^~~~
prog.cpp:16:13: note: candidates are: node* prev
class node* prev=NULL;
^~~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:66:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/istream:38,
from /usr/include/c++/6/sstream:38,
from /usr/include/c++/6/complex:45,
from /usr/include/c++/6/ccomplex:39,
from /usr/include/x86_64-linux-gnu/c++/6/bits/stdc++.h:52,
from prog.cpp:1:
/usr/include/c++/6/bits/stl_iterator_base_```