【发布时间】:2013-02-24 00:58:08
【问题描述】:
我正在设置一个哈希表,并且我已经计算了密钥,并且想要确定数组中的那个点是否已经有一些东西。我想我应该检查它是否为空,以查看空间是否为空或是否已被删除。 del.compare 是检查名字字符串是否被"DEL" 替换,这是我如何标记已删除并准备重写的元素。
我收到一个错误:
error: no match for ‘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:
这是导致错误的比较所在的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
}
}
【问题讨论】:
标签: c++ string pointers struct hashtable