C++ STL - Inserting a custom class as a mapped value -
i have class:
class monster : public player { public: // copy constructor - used populating monster list in game class monster(int newtype) { type = newtype; canmove = true; notforward = false; } int type; bool operator==(const monster& m) const {return type == m.type;} bool operator!=(const monster& m) const {return type != m.type;} bool operator< (const monster& m) const {return type < m.type;} bool operator<=(const monster& m) const {return type <= m.type;} bool operator> (const monster& m) const {return type > m.type;} bool operator>=(const monster& m) const {return type >= m.type;} };
some variables (like canmove , notforward) inherited. next, in different class, create map of monsters:
map<pair<int, int>, monster> monsters; // pair used x , y coordinate monsters.insert(make_pair(10, 10), monster(10)); // error - no instance of overloaded function
how can monster instances monsters map? added operator overloads insert, doesn't work!
simple way is
monsters[make_pair(10, 10)] = monster(10);
another way is
monsters.insert(make_pair(make_pair(10, 10), monster(10)));
yet is
monsters.insert(map<pair<int, int>, monster>::value_type(make_pair(10, 10), monster(10)));
operator overloads unnecessary, need overload operator< key of map not value. think maybe got confused because 2 calls make_pair necessary in second case.
Comments
Post a Comment