检查数组中的字符串是否为null时出错

时间:2013-02-24 00:58:09

标签: c++ string pointers struct hashtable

我正在设置哈希表,并且我已经计算了密钥,并且想要确定数组中的那个点是否已经包含了某些内容。我想我应该检查它是否为空,以查看空间是否为空或是否已被删除。 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对应于指向字符串结构的指针,初始化为10studentList[0].FIRST应该是10数组中第一个条目的第一个名称,它应该为空。

这是我在构造函数中声明它的方式:

studentList = new studentEntry[10];

编辑FIRST

FIRSTStudents结构内的studentEntry类中声明。那部分看起来像:

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
    }
}

1 个答案:

答案 0 :(得分:1)

编译器说del.compare的结果不能与0

相比

你没有展示del.compare的任何内容,所以不能说更多

目前缺少FIRST ...

的声明

以后某个时间 - FIRST现在显示为string,大概是std::string

您无法将std::string与0或nullptr进行比较。

但您可以将其length()与0比较,例如。