<分区>
我正在设置一个哈希表,我已经计算出 key ,并想确定数组中的那个位置是否已经包含某些内容。我想我应该检查它是否为 null 以查看该空间是否为空或是否已被删除。 del.compare
是检查first name字符串是否被"DEL"
替换,我就是这样标记已经删除准备重写的元素.
我收到一个错误:
错误:'operator!=' in '(((Student*)this)->Student::studentList + ((unsigned int)(((unsigned int)((Student*)this) 不匹配)->Student::key) * 20u)))->Student::studentEntry::FIRST != 0'
对于这一行:
if(studentList[key].FIRST != NULL || del.compare(studentList[key].FIRST) != 0)
在这种情况下使用 !=
有什么问题?
编辑del.compare
:
我只是使用字符串库比较,在这种情况下 del
在函数中初始化:
string del("DEL");
和studentList[key].FIRST
对应一个指向字符串结构的指针,初始化为10
。 studentList[0].FIRST
应该是 10
数组中第一个条目的名字,它应该是空的。
这是我在构造函数中声明它的方式:
studentList = new studentEntry[10];
编辑FIRST
:
FIRST
在 studentEntry
结构内的 Students
类中声明。那部分看起来像:
class Student
{
private:
struct studentEntry
{
string FIRST;
};
studentEntry *studentList;
int key;
int index;
public:
void add(string &firstname);
Student();
};
这是 Student
类的构造函数,它也可能是相关的:
Student::Student()
{
studentList = new studentEntry[10];
key = 0;
index = 0;
}
编辑添加
:
下面是 add
函数,其中进行了导致错误的比较:
void Student::add(string &firstname)
{
string del("DEL");
//find key to know which bucket to use
key = index % 10;
//check if spot in studentList is empty or has been removed
if(studentList[key].FIRST != NULL || del.compare(studentList[key].FIRST) != 0)
{
//do stuff
}
}