1

所以我不断收到错误消息:

'(&__first) >std::_List_iterator<_Tp>::operator* 中的 'operator==' 与 _Tp = Course == __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;
}
4

1 回答 1

2

find用于operator==将您要查找的参数与迭代器的值进行比较。你的Course班级没有这样operator==的。你需要实现它。

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

正如 James Kanze 指出的那样:您还可以使用std::find_if并提供一个比较函子/lambda。

于 2013-05-14T13:59:53.930 回答