Я пытаюсь передать перегрузку пользовательского класса в качестве компаратора std::map
.Ошибка при попытке передать '==' перегрузку в std :: map compator
Вот код, и ошибка, чтобы помочь вам помочь мне:
Вот Position.hpp
#ifndef POSITION_HPP_
# define POSITION_HPP_
class Position
{
public:
int _x;
int _y;
Position(){}
Position(int x, int y)
{
_x = x;
_y = y;
}
void setPosition(Position &newpos)
{
_x = newpos._x;
_y = newpos._y;
}
int getX()
{
return _x;
}
int getY()
return _y;
}
void setX(int x)
{
_x = x;
}
void setY(int y)
{
_y = y;
}
struct cmpIsSame{
inline bool operator==(const Position& pos)
{
if (pos._x == _x && pos._y == _y)
return true;
return false;
}
};
inline Position& operator=(const Position& pos)
{
_x = pos._x;
_y = pos._y;
return *this;
}
};
#endif
А вот карта заявление в GameEngine.hh
private:
std::map<Position, Case, Position::cmpIsSame> _cases;
И здесь приведена ошибка:
Position.hpp: In member function ‘bool Position::cmpIsSame::operator==(const Position&)’:
Position.hpp:17:7: error: invalid use of non-static data member ‘Position::_x’
int _x;
^
Position.hpp:52:21: error: from this location
if (pos._x == _x && pos._y == _y)
^
Position.hpp:18:7: error: invalid use of non-static data member ‘Position::_y’
int _y;
^
Position.hpp:52:37: error: from this location
if (pos._x == _x && pos._y == _y)
^
Любая помощь будет приятной?
Есть ли причина, по которой вы не хотите просто перегружать 'operator == (const Position &, const Position &)'? – TartanLlama