正在将对象指针数组初始化为NULL



我有一个项目,需要创建一个对象指针数组,该数组必须通过init((方法初始化为NULL。这是代码:

HashLin *primHash[10];
void HashPerfect:: init() {         //FIX ME
for (int i=0; i<10; i++){
primHash[i] = NULL;
}

当我试图编译代码时,我收到了以下错误消息:

hashperfect.cpp: In member function 'void HashPerfect::init()':
hashperfect.cpp:17:15: warning: passing NULL to non-pointer argument 1 of 
'HashLin::HashLin(int)' [Wconversion-null]
primHash[i] = NULL;
^

我觉得这可能与我的类HashLin的构造函数有关,但我不确定。如果这有助于诊断问题,下面是类HashLin:的构造函数

HashLin::HashLin(){
//size = 0;
hashTable = vector<string>(1);
init();
};
HashLin::HashLin(int mySize){
hashTable = vector<string>(mySize);
init();
};

非常感谢您的帮助!谢谢

我的意图是创建一个指向HashLin对象的指针数组——这是HashLin目标类的代码。。。

#include <vector>
#include <string>
#include <iostream>
#include "HashLin.hpp"
using namespace std;

bool success;
vector<string> hashTable;
void HashLin:: init() {
//hashTable = vector<string>(1);
for (int i=0; i<hashTable.size(); i++)
hashTable[i] = "";
}       
int HashLin:: hashMe(string myString) {
//char foo[] = myString;

char *s = new char[(myString.length())];
unsigned int hash;
for (hash = 0; *s != ''; s++)
hash = 37*hash + *s;
hash = hash % hashTable.size();

return hash;
}


HashLin::HashLin(){
//size = 0;
hashTable = vector<string>(1);
init();
};
HashLin::HashLin(int mySize){
hashTable = vector<string>(mySize);
init();
};
bool HashLin:: insert(string insertMe){
//hashMe(insertMe);
int hashVal = hashMe(insertMe);
success = false;
int count = 0;
while (hashVal < hashTable.size() && success==false && 
count<hashTable.size()) {
if (hashTable[hashVal] == ""){
hashTable[hashVal] = insertMe;
success = true;
}
else if (hashVal == hashTable.size()-1)
hashVal = 0;
else{
hashVal++;
count++;
}   

}
if (count == hashTable.size())
cout << "Failed to Hash: " << insertMe << endl;
return success;
}
void HashLin:: printOut(){
for (int i=0; i<hashTable.size(); i++){
cout << i << ": " << hashTable[i] << endl;
}
}
int HashLin:: getSize(){
return hashTable.size();
}
bool HashLin:: getSuccess(){
return success;
}
void HashLin:: setSize(int toSet){
hashTable.resize(toSet);
}

尝试将对象设置为nullptr:primHash[i] = nullptr

相关内容

  • 没有找到相关文章

最新更新