【问题标题】:error when checking if string in array is null [closed]检查数组中的字符串是否为空时出错[关闭]
【发布时间】: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 对应一个指向字符串结构的指针,初始化为10studentList[0].FIRST 应该是10 数组中第一个条目的名字,并且应该为空。

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

studentList = new studentEntry[10];

编辑FIRST:

FIRSTstudentEntry 结构内的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


    【解决方案1】:

    编译器说del.compare的结果不能和0比较

    del.compare你没有显示任何东西,所以不能说更多

    目前缺少FIRST的声明...


    一段时间后 - FIRST 现在被发现是 string,大概是 std::string

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

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

    【讨论】:

    • 我更新了问题,对不起,我以为问题出在||的前半部分
    • @manalishi:显然我当时的回答是错误的。请展示一个完整的小示例程序。
    • 好的,我添加了更多关于FIRST 的信息以及我使用的类和构造函数
    • 谢谢,原来如此。改成studentList[key].FIRST.length() != 0
    猜你喜欢
    • 1970-01-01
    • 2016-05-15
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    相关资源
    最近更新 更多