以下は、スキップリストのテンプレート化されたクラス内の内部ネストされたクラスです。私は次のように宣言する行に "このスコープで高さが宣言されていません"というエラーが表示されます。エラー:非静的データメンバーの無効な使用、変数がこのスコープで宣言されていません
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;
}