Error: Invalid use of non-static data member, variable was not declared in this scope
Error: Invalid use of non-static data member, variable was not declared in this scope
以下是跳过列表的模板化 class 中的内部嵌套 class。我收到错误:"height was not declared in this scope" 在声明 *next.
的行
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;
};
将 value 和 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;}
};
我不知道该怎么做才能解决这个问题。这是整个 class:
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;
}
问题 1
你不能使用编译器还没有看到的东西,所以把声明移到用法上面解决了这个问题。你已经这样做了。干得好。
问题 2
height
只有在构造对象后才能知道。如果对象的大小未知,则无法构造对象,并且 height
决定 next
的大小,这有助于对象的大小。赶上 22.
建议使用 std::vector
代替数组,并在构造函数的 Member Initializer List 中使用 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)
{
}
在下面一行
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 我不确定什么数据结构使用指针数组作为节点的 "next" 节点。只是想一想。
以下是跳过列表的模板化 class 中的内部嵌套 class。我收到错误:"height was not declared in this scope" 在声明 *next.
的行 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;
};
将 value 和 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;}
};
我不知道该怎么做才能解决这个问题。这是整个 class:
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;
}
问题 1
你不能使用编译器还没有看到的东西,所以把声明移到用法上面解决了这个问题。你已经这样做了。干得好。
问题 2
height
只有在构造对象后才能知道。如果对象的大小未知,则无法构造对象,并且 height
决定 next
的大小,这有助于对象的大小。赶上 22.
建议使用 std::vector
代替数组,并在构造函数的 Member Initializer List 中使用 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)
{
}
在下面一行
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 我不确定什么数据结构使用指针数组作为节点的 "next" 节点。只是想一想。