2013-05-14 24 views
1

所以我一直被調用錯誤信息上:錯誤調用時的std ::找到一個用戶定義的對象

敵不過 '==操作符' 中的「(& __first)>的std :: _ List_iterator < _TP> ::與_TP =場== __val」

在下面的代碼

運算符*:

int main(){ 
    Course NOT_FOUND("NOT_FOUND", "NOT_FOUND", 0); 
    Course x("COMP2611", "COMPUTER ORGANIAZTION", 2); 
    HashTable<Course> q(NOT_FOUND, 17, 36); 

    q.insert(x); 
} 



template <class HashedObj> 
class HashTable{ 
public: 
    HashTable(const HashedObj& notFound, int bucket, int base); 
    void insert(const HashedObj& x); 

private: 
    const HashedObj ITEM_NOT_FOUND; 
    vector<list<HashedObj> > theList; 
}; 

template <class HashedObj> 
void HashTable<HashedObj>::insert(const HashedObj& x){ 
    list<HashedObj>& whichList = theList[hash(x)]; 
    typename list<HashedObj>::iterator itr; 
    itr = std::find(theList[0].begin(), theList[0].end(), x); 
    if(itr == theList[hash(x)].end()) 
    whichList.insert(theList[hash(x)].begin(), x); 
} 

我測試和理解錯誤是從線

itr = std::find(theList[0].begin(), theList[0].end(), x); 

但我不知道如何解決它。我想我只是在這裏調用 標準查找功能,但顯然它不起作用。

課程定義正確我認爲,因爲我在之前的其他課上進行過測試。

的代碼是:

class Course{ 
public: 
Course(string code, string name, int credit): 
    _code(code), _name(name), _credit(credit){} 

string _code; 

private: 
string _name; 
int _credit; 

friend int hash(Course x); 
}; 


int hash(Course x){ 
    int sum = 0; 
    for(int i = 0; i < x._name.size(); i++) 
     sum+=_name[i]; 

    return sum % 35; 
} 

回答

2

find使用operator==來比較你正在尋找的迭代器的值的參數。你的Course班沒有這樣的operator==。你需要實現它。

class Course { 
public: 
    // bla bla 
    friend 
    bool operator==(const Course& x, const Course& y) 
    { return your-comparison-code-here; } 
}; 

正如詹姆斯甘孜指出:您還可以使用std::find_if,並提供一個比較仿函數/λ。

+1

或者,他可以使用'find_if',並提供一個比較器。 – 2013-05-14 14:11:06

+0

非常感謝你們,這非常有幫助! – user1819047 2013-05-15 16:38:08

+0

@ user1819047如果此答案解決了您的問題,請點擊複選標記接受此問題。也可以查看[faq](http://stackoverflow.com/faq)來了解stackoverflow。 – pmr 2013-05-15 19:29:46

相關問題