I'm trying to pass an overload of a custom class as a comparator of an std::map
.
Here's some code and the error to help you help me :
Here is 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
And here is the map declaration in GameEngine.hh
private:
std::map<Position, Case, Position::cmpIsSame> _cases;
And here is the error:
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)
^
Any help would be a pleasure?
As to the error
In the implementation of the
operator==
you attempt to access members_x
etc. of the enclosing class. You don't have access to them there. Embedding a class in a class is a namespace issue, not a member access issue.It would be easier to just provide a free function or a functor that offers the comparison.
As to the comparison of keys in
map
The definition of the key comparison for
std::map
isstd::less
which by default callsoperator<
. It may be easier to just implement an overloadedoperator<
for the key you use.As to the implementation of
less
This is really up to you and manner in which you use it in the application. But as a first off approximation (also to try get the code up and going), consider using the distance (pythagorean theory) of the point to the origin (probably 0,0).