我见过其他人的问题,但没有发现适用于我在这里想要实现的目标。
我正在尝试使用 std::sort 和 a 通过我的 EntityManager 类对实体进行排序std::vector<Entity *>
/*Entity.h*/
class Entity
{
public:
float x,y;
};
struct compareByX{
bool operator()(const GameEntity &a, const GameEntity &b)
{
return (a.x < b.x);
}
};
/*Class EntityManager that uses Entitiy*/
typedef std::vector<Entity *> ENTITY_VECTOR; //Entity reference vector
class EntityManager: public Entity
{
private:
ENTITY_VECTOR managedEntities;
public:
void sortEntitiesX();
};
void EntityManager::sortEntitiesX()
{
/*perform sorting of the entitiesList by their X value*/
compareByX comparer;
std::sort(entityList.begin(), entityList.end(), comparer);
}
我收到了十几个错误,例如
: error: no match for call to '(compareByX) (GameEntity* const&, GameEntity* const&)'
: note: candidates are: bool compareByX::operator()(const GameEntity&, const GameEntity&)
我不确定,但 ENTITY_VECTOR 是std::vector<Entity *>
,我不知道在使用 compareByX 函数对象时是否会出现问题?
我对 C++ 很陌生,所以欢迎任何形式的帮助。