2012-12-03 221 views
0

所以我想在C++中编写一个基本的红黑树。这是我从C到C++的第一次真正的转换,我遇到了一个范围问题。我试图使用一个全局变量来跟踪树的根,因为我很懒,不想让每个函数都传递旧的或新的根。无论如何,我有任何函数或类之外声明的指针(名为root),在我看来,任何人都应该看到它。当我编译,我得到:C++全局变量超出了范围

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 

所以,里面的类的功能无法看到全局变量,但主要功能。我需要做什么才能让类内的函数看到这个变量?提前致谢。最大

#include <iostream> 
#include <cstdio> 
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() && num == -1){//if first insertion 
     num = newNum; 
     color = BLACK; 
     root = this; 
    }else if(newNum < num) 
     if(left == NULL){ 
      left = new node; 
      left->parent = this; 
      left->num = newNum; 
      left->fixColor(); 
     }else 
      left->ins(newNum); 
    else 
     if(right == NULL){ 
      right = new node; 
      right->parent = this; 
      right->num = newNum; 
      right->fixColor(); 
     }else 
      right->ins(newNum); 
}; 
int uncleColor(void){ 
    if(parent != NULL){ 
     if(parent->parent != NULL){ 
      node *grandparent = parent->parent; 
      if(grandparent->left == parent) 
       if(grandparent->right == NULL) 
        return BLACK; 
       else 
        return grandparent->right->color; 
      else{ 
       if(grandparent->left == NULL) 
        return BLACK; 
       else 
        return grandparent->left->color; 
      } 
     }else 
      cout << "[Error] Grandparent DNE, at root\n"; 
    }else 
     cout << "[Error] Parent DNE, at root\n"; 
    return -1; 
}; 
void fixColor(void){ 
    if(isRoot()){ 
     color = BLACK; 
    }else if(color == RED){ 
     if(parent->color == RED){//if parent is black, nothing violated 
      int uncle = uncleColor(); 
      if(uncle == RED){//uncle is red too 
       case1(); 
       parent->parent->fixColor();//call recursivly on grandparent 
      }else if(uncle == BLACK){//uncle is black 
       case2();//case 2 will then call case 3 
      }else 
       cout << "[Error] fixColor uncle color mismatch\n"; 
     } 
    }else 
     cout << "[Error] fixcolor node is black?\n"; 
}; 
void case1(void){ 
    node *grandparent = parent->parent; 
    node *uncle = NULL; 
    if(grandparent->left == parent) 
     uncle = grandparent->right; 
    else 
     uncle = grandparent->left; 
    uncle->color = BLACK; 
    parent->color = BLACK; 
    grandparent->color = RED; 
}; 
void case2(void){ 
    node *grandparent = parent->parent; 
    if(this == parent->right && parent == grandparent->left){ 
      rotate_left(); 
      left->case3(); 
    }else if(this == parent->left && parent == grandparent->right){ 
      rotate_right(); 
      right->case3(); 
    }else 
     case3(); 
}; 
void case3(void){ 
    node *grandparent = parent->parent; 
    parent->color = BLACK; 
    color = RED; 
    if(this == parent->left) 
      grandparent->rotate_right(); 
    else 
      grandparent->rotate_left(); 
    if(parent->isRoot()) 
     root = parent; 
}; 
void rotate_left(void){ 
    node *grandparent = parent->parent; 
    grandparent->left = this; 
    parent->right = this->left; 
    this->left = parent; 
    parent->parent = this; 
    parent = grandparent; 
}; 
void rotate_right(void){ 
    node *grandparent = parent->parent; 
    grandparent->right = this; 
    parent->left = this->right; 
    this->right = parent; 
    parent->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", &READ_task, &READ_val) == 2){ 
     if(READ_task == 'i') 
      root->ins(READ_val); 
     else if(READ_task == 'd') 
      root->del(READ_val); 
     else 
      cout << "Instruction from file not i or d\n"; 
    } 
} 
return 0; 
} 
+0

您有一个树节点的类。您应该为整个树添加一个类。根将是第二个类的成员变量。 –

回答

3

你必须声明它在使用它的每个翻译单元:

extern node* root; 

在你的情况,你还需要一个向前声明:

class node; 
extern node* root; 

class node{ 
//.......... 

请注意,这种风格不是惯用的C++,它只是C和一些C++特性。我会开始用一本书学习C++。

2

将声明root放在使用它的代码之前。

0

您还可以为有问题的函数添加参数,并在调用成员函数时将该变量用作所述参数