C++ Vector Find() Overloaded operator - Error -
i'm receiving undefined symbols architecture error when compiling this.
have use vector, find() algorithm, , non-member overloaded operator function.
some guidance around error appreciated.
#include <stdio.h> #include <iostream> #include <stdexcept> #include <vector> #include <algorithm> #include <string> using namespace std; struct person { char firstinitial; string firstname; person(const char fi, const string fn) { firstinitial = fi; firstname = fn; }; char getinitial() { return firstinitial; }; string getname() { return firstname; }; bool operator==(const person& r); }; bool operator==(const person& r, const person& x) { return x.firstinitial == r.firstinitial; } int main (int argc, char *argv[]) { vector<person> myvector; vector<person>::iterator itr; myvector.push_back(person('j', "john")); myvector.push_back(person('s', "steve")); myvector.push_back(person('c', "candice")); itr = find (myvector.begin(), myvector.end(), person('s', "")); if (itr != myvector.end()) cout << "first name: " << itr->getname() << '\n'; else cout << "not found" << '\n'; }
you need define comparator outside of class, not member function
class person { public: friend bool operator==(const person &a, const person &b); } bool operator==(const person& r, const person& x) { return x.firstinitial == r.firstinitial; }
and should work, program returned "steve" me.