c++ - Finding a classmember in a vector of other class members -
i have classes team , player. every team member has object vector playerlist players stored in. every player has attributes id. want transfer player team . created function this.
first want findout if player b maybe member of team a, in case message should printed out. failed in writing function this.
here try(the search starts in if loop, want find out if player b member of team a)
#include <vector> #include <string> #include <iostream> using namespace std; class player { public: private: }; class team { public: vector<player> getplayerlist(){ return playerlist; } string getteamname(){ return teamname; } void playerbuy(team a, player b) { vector<player> playerlist; playerlist = a.getplayerlist(); if (find(playerlist.begin(), playerlist.end(), 5) != playerlist.end()) { } else { cout << "this player not member of " << a.getteamname(); } } private: vector<player> playerlist; string teamname; }; int main() { return 0; } i got error c2678: binary '==' : no operator found takes left-hand operand of type 'player' (or there no acceptable conversion). figured out comes line
if (find(playerlist.begin(), playerlist.end(), 5) != playerlist.end()) the =! playerlist.end() seems wrong there. part of code copy of code don't know does. should put in there instead ? , 5 means ?
the error giving hint: need implement equality operator class player. std::find uses determine if element has been found.
alternatively, can use std::find_if custom unary predicate.
Comments
Post a Comment