错误:非静态数据成员的使用无效,在此范围内未声明变量

Error: Invalid use of non-static data member, variable was not declared in this scope

本文关键字:范围内 变量 未声明 无效 静态 数据成员 错误      更新时间:2023-10-16

以下是Skiplist模板类中的内部嵌套类。我遇到了错误:"在此范围中没有声明"高度"在"宣布 *"的行中。

        class Node{
            public:
                Node(int height){
                    this->height = height;
                }
                Node(Key_t key, Mapped_t obj, int height){
                    value = std::make_pair(key, obj);
                    this->height = height;
                }
                SkipList<Key_t, Mapped_t>::Node *next[height];
                int getHeight(){return height;}
                Key_t getKey(){return value.first;}
                Mapped_t getObj(){return value.second;}
            private:
                std::pair<Key_t, Mapped_t> value;
                int height;
        };
如下
         class Node{
                private:
                    std::pair<Key_t, Mapped_t> value;
                    int height;
                public:
                    Node(int height){
                        this->height = height;
                    }
                    Node(Key_t key, Mapped_t obj, int height){
                        value = std::make_pair(key, obj);
                        this->height = height;
                    }
                    SkipList<Key_t, Mapped_t>::Node *next[height];
                    int getHeight(){return height;}
                    Key_t getKey(){return value.first;}
                    Mapped_t getObj(){return value.second;}
            };

,我对解决这个问题的操作感到不知所措。这是整个课程:

template <class Key_t, class Mapped_t>
class SkipList{
public:
    SkipList(int prob, int max){
        probOutOf100 = prob;
        maxHeight = max;
        head = new SkipList<Key_t, Mapped_t>::Node(maxHeight);
        tail = new SkipList<Key_t, Mapped_t>::Node(maxHeight);
        for(int i = 0; i < maxHeight; i++){
            head->next[i] = tail;
        }
    }
    ~SkipList(){
        delete head;
        delete tail;
    }
    class Node{
        public:
            Node(int height){
                this->height = height;
            }
            Node(Key_t key, Mapped_t obj, int height){
                value = std::make_pair(key, obj);
                this->height = height;
            }
            SkipList<Key_t, Mapped_t>::Node *next[height];
            int getHeight(){return height;}
            Key_t getKey(){return value.first;}
            Mapped_t getObj(){return value.second;}
        private:
            std::pair<Key_t, Mapped_t> value;
            int height;
    };
    Node *head;
    Node *tail;
    int probOutOf100;
    int maxHeight;
}

的行中
SkipList<Key_t, Mapped_t>::Node *next[height];

您正在尝试声明一系列指针。但是,除非height在编译时已知,否则您不能这样做。

您可能需要将其更改为vector并在运行时进行初始化。

std::vector<SkipList<Key_t, Mapped_t>::Node*> next;

...
Node(int height) : next(height) {
    this->height = height;
}

ps 我不确定哪种数据结构使用了哪种指针作为节点的"下一个"节点。只是要考虑的东西。

问题1

您无法使用编译器尚未看到的内容,因此将声明上方移动来解决此问题。您已经完成了。好工作。

问题2

height直到对象构造后才知道。如果对象的大小未知,则无法构造对象,并且height确定有助于对象大小的next的大小。捕获22。

建议使用std::vector代替数组,并在构造函数成员初始化器列表中使用height初始化向量。示例:

std::vector<SkipList<Key_t, Mapped_t>::Node *> next;
Node(int height): height(height), next(height)
{
}
Node(Key_t key, Mapped_t obj, int height): 
    value(std::make_pair(key, obj)),
    height(height), 
    next(height)
{
}