这是我的结构:
struct HashTable{
int tsize;
int count; //No. of elements in table
struct HashTableNode **Table;
};
这就是我想要做的事情:
struct HashTable *h=(struct HashTable *)malloc(sizeof(struct HashTable));
if(!h) return NULL;
h->tsize=size/LOAD_FACTOR;
h->count=0;
h->Table=(struct HashTableNode**)malloc(sizeof(HashTableNode *)*h->tsize);//this line
我无法使用new运算符
将最后一行转换为等效的C ++版本解决了
我将代码更改为:
h->Table=new HashTableNode*[h->tsize];
for(int i=0;i<h->tsize;i++)
h->Table[i]=new HashTableNode;
我自己做了一点调试,瞧。它解决了。
谢谢大家。
答案 0 :(得分:0)
显然我同意你的问题中的大多数评论,但是如果你的代码中真的需要指针,你的问题的解决方案可能如下所示。
使用typedef说明符定义类型:
typedef HashTableNode* PHashTableNode;
然后:
h->Table=new PHashTableNode[h->tsize];