0

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

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;
}
4

3 回答 3

3

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

extern node* root;

In your case, you'll also need a forward declaration:

class node;
extern node* root;

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

Note that this style isn't idiomatic C++, it's just C with some C++ features. I'd start learning C++ with a book.

于 2012-12-03T01:43:04.977 回答
2

Put the declaration of root before the code that uses it.

于 2012-12-03T01:46:04.087 回答
0

You could also add a parameter to the functions in question and use the variable as said parameter when calling the member function

于 2012-12-03T02:58:10.887 回答