简体   繁体   English

无法在C ++中初始化指针数组

[英]Can't initialize an array of pointers in C++

I have an array of pointers to a structure called "table" (the structure is called Node). 我有一个指向名为“table”的结构的指针数组(该结构称为Node)。

I declare the array as so in the class: 我在类中声明了数组:

Node * table;

Then, in another method, I initalize the table: 然后,在另一种方法中,我将表初始化:

this->table = new Node [this->length];

And everything works fine. 一切正常。 this->length is a valid entry, this->table is pointing to the right array, and etc. However, then I try to change the value of the elements: this-> length是一个有效的条目,this-> table指向正确的数组,等等。但是,然后我尝试更改元素的值:

for(int i = 0; i < this->length; i++) {
    this->table[i] = new Node;
}

Or even 甚至

for(int i = 0; i < this->length; i++) {
    this->table[i] = 0;
}

And everything starts bugging out. 一切都开始了。 Why can't I set these pointers to anything? 为什么我不能将这些指针设置为任何东西?

This is the error I get: 这是我得到的错误: 在此输入图像描述

(Where line 15 is the line of "this->table[i] = new Node;"). (其中第15行是“this-> table [i] = new Node;”的行)。

I hate to post long segments of code, so here's a shortened version of the code itself: 我讨厌发布长段代码,所以这里是代码本身的缩短版本:

template <class T, class S>
class HashMap {
    public:
        HashMap();
    private:
        struct Node;
        Node * table;
        static const unsigned length = 100;
};

template <class T, class S>
HashMap<T, S>::HashMap() {
    this->table = new Node [HashMap::length];
    for(int i = 0; i < HashMap::length; i++) {
        this->table[i] = new Node;
    }
}

template <class T, class S>
struct HashMap<T, S>::Node {
    T value;
    S key;
    Node * next;
};

No research I'm doing is telling me what the error is; 我正在做的研究没有告诉我错误是什么; any help is appreciated! 任何帮助表示赞赏!

You don't have an array of pointers. 你没有一个指针数组。 You have an array of Nodes. 你有一个节点数组。 Apparently, what you want is something like this: 显然,你想要的是这样的:

Node ** table;
...
this->table = new Node*[this->length];

Or maybe you don't actually need an array of pointers, but simply an array of nodes. 或许你实际上并不需要一个指针数组,而只需要一个节点数组。 In that case, no extra initialization is needed beyond: 在这种情况下,除了之外不需要额外的初始化:

this->table = new Node[this->length];

Beyond that, unless this is a learning exercise, take a look at the standard library , which has dynamic arrays and hash maps all ready for you. 除此之外,除非这是一个学习练习,否则请查看标准库 ,其中包含动态数组哈希映射

table is not an array of pointers. table不是指针数组。 It's an array of Node s (or rather, it points to an array of Node s). 它是一个Node数组(或者更确切地说,它指向一个Node数组)。 The type of table is Node* ; table的类型是Node* ; the type of table[i] is Node , not Node* . table[i]的类型是Node ,而不是Node*

If you actually do want an array of pointers, then you need 如果你确实想要一个指针数组,那么你需要

Node** table;
table = new Node*[length];

Or better still, something like 或者更好的是,像

vector<unique_ptr<Node>> table;
table.resize(length);

You do not have declared an array of pointers. 您没有声明指针数组。
Node *table(point to a node) 节点*表(指向节点)
Node **table(point to an array Nodes) 节点**表(指向数组节点)

Node** table;
table =new Node*[this->length];
for(int i=0;i<this->length;i++)
{
   table[i]=new Node;
} 
this->table = new Node [HashMap::length];

this->table is of type Node* and new Node [HashMap::length] also returns a Node* , ie an array of Node of lenght HashMap::length is created and the array address is stored in this->table pointer. this->table的类型为Node* ,新的Node [HashMap :: length]也返回一个Node *,即创建HashMap::lengthHashMap::length的Node数组,并将数组地址存储在this->table指针中。

this->table[i] = new Node;

As an example, we can define: 作为一个例子,我们可以定义:

int* arr = new int[10];

Here arr is of type int* but arr[0] will be of type int. 这里arr的类型为int *,但arr[0]的类型为int。

similarly, this->table[i] is of type Node and new Node returns Node*. 类似地, this->table[i]是Node类型,新Node返回Node *。 Hence incompatible types. 因此不兼容的类型。 Correct line would be 正确的路线是

this->table[i] = *new Node;

But, this line is unnecessary as the array of Nodes is already created and the memory is allocated. 但是,这条线是不必要的,因为已经创建了节点数组并且分配了内存。 Using this line in the code will lead to a memory leak. 在代码中使用此行将导致内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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