简体   繁体   中英

c++ global variable out of scope in class

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:

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

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

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

You have to declare it in each translation unit that uses it:

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.

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

您也可以在相关函数中添加参数,并在调用成员函数时将变量用作所述参数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM