2013-02-24 10 views
0

我設置了一個哈希表,並且計算了該鍵,並且想要確定該數組中的該點是否已經有內容。我想我應該檢查它是否爲null,以查看該空間是否爲空或者是否已被刪除。 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相當於一個指向一個struct字符串,初始化爲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 
    } 
} 

回答

1

編譯器說的del.compare結果不具有可比性0

您還沒有表現出約del.compare什麼,所以更不能說

目前缺乏FIRST&hellip聲明;


不多久– FIRST現在顯露是string,想必std::string

您無法將std::string設置爲0或設置爲nullptr

但是,例如,您可以將其length()與0比較。

+0

我更新了這個問題,對不起,我以爲問題出現在'||' – manalishi 2013-02-24 01:26:45

+0

@manalishi的前半部分:顯然我的回答是錯誤的。請顯示一個完整的小例子程序。 – 2013-02-24 01:36:24

+0

好的,我添加了更多關於'FIRST'的信息以及我使用 – manalishi 2013-02-24 01:51:49

相關問題