title: c++ global variable out of scope in class
body: <p>So I'm trying to write a basic red black tree in c++. This is my first real transition from C to C++ and I'm running into a scope issue. I'm trying to use a global variable to keep track of the root of the tree because I'm lazy and don't want to have every function pass the old or new root back. Anyways, I have the pointer (named root) declared outside of any function or class and in my mind, should be visible to anyone. When I compile, I get:</p>
  
  <pre><code>main.cpp: In member function ‘void node::ins(int)’:
  main.cpp:23:4: error: ‘root’ was not declared in this scope
  main.cpp: In member function ‘void node::case3()’:
  main.cpp:110:4: error: ‘root’ was not declared in this scope
  </code></pre>
  
  <p>So, the functions inside the class can't see the global variable but the main function can. What do I have to do to let the functions inside the class see this variable? Thanks in advance. Max</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdio&gt;
  using namespace std;
  
  const int BLACK = 0;
  const int RED = 1;
  
  class node{
    public:
  node *parent = NULL;
  node *left = NULL;
  node *right = NULL;
  int color = RED;
  int num = -1;
  int isRoot(void){
      if(parent == NULL) return 1;
      else             return 0;
  };
  void ins(int newNum){
      if(isRoot() &amp;&amp; num == -1){//if first insertion
          num = newNum;
          color = BLACK;
          root = this;
      }else if(newNum &lt; num)
          if(left == NULL){
              left = new node;
              left-&gt;parent = this;
              left-&gt;num = newNum;
              left-&gt;fixColor();
          }else
              left-&gt;ins(newNum);
      else
          if(right == NULL){
              right = new node;
              right-&gt;parent = this;
              right-&gt;num = newNum;
              right-&gt;fixColor();
          }else
              right-&gt;ins(newNum);
  };  
  int uncleColor(void){
      if(parent != NULL){
          if(parent-&gt;parent != NULL){
              node *grandparent = parent-&gt;parent;
              if(grandparent-&gt;left == parent)
                  if(grandparent-&gt;right == NULL)
                      return BLACK;
                  else
                      return grandparent-&gt;right-&gt;color;
              else{
                  if(grandparent-&gt;left == NULL)
                      return BLACK;
                  else
                      return grandparent-&gt;left-&gt;color;
              }
          }else
              cout &lt;&lt; "[Error] Grandparent DNE, at root\n";
      }else
          cout &lt;&lt; "[Error] Parent DNE, at root\n";
      return -1;
  };
  void fixColor(void){
      if(isRoot()){
          color = BLACK;
      }else if(color == RED){ 
          if(parent-&gt;color == RED){//if parent is black, nothing violated
              int uncle =  uncleColor();
              if(uncle == RED){//uncle is red too
                  case1();
                  parent-&gt;parent-&gt;fixColor();//call recursivly on grandparent
              }else if(uncle == BLACK){//uncle is black
                  case2();//case 2 will then call case 3
              }else
                  cout &lt;&lt; "[Error] fixColor uncle color mismatch\n";
          }
      }else
          cout &lt;&lt; "[Error] fixcolor node is black?\n";
  };
  void case1(void){
      node *grandparent = parent-&gt;parent;
      node *uncle = NULL;
      if(grandparent-&gt;left == parent)
          uncle = grandparent-&gt;right;
      else
          uncle = grandparent-&gt;left;
      uncle-&gt;color = BLACK;
      parent-&gt;color = BLACK;
      grandparent-&gt;color = RED;
  };
  void case2(void){
      node *grandparent = parent-&gt;parent;
      if(this == parent-&gt;right &amp;&amp; parent == grandparent-&gt;left){
              rotate_left();
              left-&gt;case3();
      }else if(this == parent-&gt;left &amp;&amp; parent == grandparent-&gt;right){
              rotate_right();
              right-&gt;case3();
      }else
          case3();
  };
  void case3(void){
      node *grandparent = parent-&gt;parent;
      parent-&gt;color = BLACK;
      color = RED;
      if(this == parent-&gt;left)
              grandparent-&gt;rotate_right();
      else
              grandparent-&gt;rotate_left();
      if(parent-&gt;isRoot())
          root = parent;
  };
  void rotate_left(void){
      node *grandparent = parent-&gt;parent;
      grandparent-&gt;left = this;
      parent-&gt;right = this-&gt;left;
      this-&gt;left = parent;
      parent-&gt;parent = this;
      parent = grandparent;
  };
  void rotate_right(void){
      node *grandparent = parent-&gt;parent;
      grandparent-&gt;right = this;
      parent-&gt;left = this-&gt;right;
      this-&gt;right = parent;
      parent-&gt;parent = this;
      parent = grandparent;
  };
  void del(int val){
  
  };
  };
  
  node *root = NULL;
  
  int main(int argc, char **argv){
  root = new node;
  char READ_task;
  int READ_val;
  FILE *txtPtr = NULL;
  if((txtPtr = fopen(argv[1],"r")) == NULL){printf("[Error] Unable to Load File: '%s'\nExiting...\n",argv[1]);}
  else{
      while(fscanf(txtPtr, "%c%d", &amp;READ_task, &amp;READ_val) == 2){
          if(READ_task == 'i')
              root-&gt;ins(READ_val);
          else if(READ_task == 'd')
              root-&gt;del(READ_val);
          else
              cout &lt;&lt; "Instruction from file not i or d\n";
      }
  }
  return 0;
  }
  </code></pre>