Gol*_*les 4 c++ operators function-object operator-keyword
我见过其他人的问题,但没有发现适用于我在这里尝试实现的目标。
我正在尝试使用 std::sort 和一个通过我的 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);
}
Run Code Online (Sandbox Code Playgroud)
我收到了十几个错误,比如
: error: no match for call to '(compareByX) (GameEntity* const&, GameEntity* const&)'
: note: candidates are: bool compareByX::operator()(const GameEntity&, const GameEntity&)
Run Code Online (Sandbox Code Playgroud)
我不确定,但 ENTITY_VECTOR 是std::vector<Entity *>,我不知道这是否是使用 compareByX 函数对象时的问题?
我对 C++ 很陌生,所以欢迎任何形式的帮助。
第三个进来......在你编辑了你的问题之后,仍然是一个开放的主题:你的比较器将 aconst &带到GameEntity课堂上。它应该,为了与工作的价值vector<GameEntity*>,采取 const GameEntity*的参数来代替。
| 归档时间: |
|
| 查看次数: |
21981 次 |
| 最近记录: |