I have this functor class :
#include <string>
using namespace std;
class IsPlayerOfType
{
public:
IsPlayerOfType(const string& type) : type_(type) {}
bool operator()(const Player* player) const
{
return (player->getType() == type_);
}
private:
string type_;
};
The class "Player" represent a player that has several methods and attributes. Among them, there is the method getType() which returns a string.
At some point of my program I have a variable called players_
which is of type vector<Player*>
Finally I have the following code to count the number of players of a certain type in my vector :
int number = count_if(players_.begin(), players_.end(), IsPlayerOfType("Defensive"));
When compiling I get a lot of errors such as :
- error C2011: 'IsPlayerOfType' : 'class' type redefinition
- error C2440: '' : cannot convert from 'const char [10]' to 'IsPlayerOfType'
error C2780: 'iterator_traits<_Iter>::difference_type std::count_if(_InIt,_InIt,_Pr)' : expects 3 arguments - 2 provided
I don't understand very well how count_if works, I tried to write this code inspiring myself from this answer : https://stackoverflow.com/a/13525420
I don't see where I'm wrong and the compiler errors confuse me.