-2

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_```
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

1 Answers1

3

Your program pits two bad and dangerous habits against each other. They both win and you lose.

  1. using namespace std is extremely bad and dangerous. Never do this, ever. This directive pollutes the (usually global) namespace wuth potentially thousands of names from the standard library, most of which you probably have no idea about. They create conflicts with your own names. Worse still, they can silently change the meaning of your program if you as much as add a completely innocent and unrelated #include directive.
  2. Global variables are bad and dangerous. They often make reasoning about programs very difficult, and tracing bugs nigh impossible. Only declare variables where they are needed. If you need to share state between functions, use parameters and return values.

In your example, the global variable prev conflicts with the standard function std::prev which you have injected into the global namespace. You could have avoided the error by avoiding either of the two bad habits, but it's a good opportunity to unlearn both. Don't miss it.

user4581301
  • 33,082
  • 7
  • 33
  • 54
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243