c ++全局变量超出了类的范围

时间:2012-12-03 01:40:56

标签: c++ class scope global-variables

所以我试图用c ++写一个基本的红黑树。这是我第一次从C到C ++的真正过渡,我遇到了一个范围问题。我正在尝试使用全局变量来跟踪树的根,因为我很懒,并且不希望每个函数都传递旧的或新的root。无论如何,我在任何函数或类之外声明了指针(名为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;
}

3 个答案:

答案 0 :(得分:3)

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

extern node* root;

在您的情况下,您还需要一份前瞻声明:

class node;
extern node* root;

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

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

答案 1 :(得分:2)

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

答案 2 :(得分:0)

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