c++ - Error when calling std::find on a user-defined object -
so keep error message called:
no match 'operator==' in '(&__first) >std::_list_iterator<_tp>::operator* _tp = course == __val'
in following code:
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); }
i tested , understand error line
itr = std::find(thelist[0].begin(), thelist[0].end(), x);
but don't know fix it. think calling standard find function here, apparently not work.
the class course defined correctly think, because tested in other classes before.
the code is:
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 = 0; < x._name.size(); i++) sum+=_name[i]; return sum % 35; }
find
uses operator==
compare parameter looking value of iterator. course
class has no such operator==
. need implement it.
class course { public: // bla bla friend bool operator==(const course& x, const course& y) { return your-comparison-code-here; } };
as james kanze points out: can use std::find_if
, provide comparison functor/lambda.
Comments
Post a Comment