c++ - Error when trying to pass '==' overload to std::map comparator -
i'm trying pass overload of custom class comparator of std::map
.
here's code , error me :
here 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 map declaration in gameengine.hh
private: std::map<position, case, position::cmpissame> _cases;
and here 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: 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: location if (pos._x == _x && pos._y == _y) ^
any pleasure?
first of have use either functional object or static function. in case approach wrong because comparator shall satisfy strict weak ordering principle correcponds operator <.
from c++ standard (23.2.4 associative containers)
3 phrase “equivalence of keys” means equivalence relation imposed comparison , not operator== on keys. is, 2 keys k1 , k2 considered equivalent if comparison object comp, comp(k1, k2) == false && comp(k2, k1) == false. 2 keys k1 , k2 in same container, calling comp(k1, k2) shall return same value.
Comments
Post a Comment