我有一堂课:
class Monster : public Player
{
public:
// Copy constructor - used for 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;}
};
一些变量(如 canmove 和 notforward)是继承的。接下来,在另一个班级中,我创建了一张怪物地图:
map<pair<int, int>, Monster> monsters; // Pair used for x and y coordinate
monsters.insert(make_pair(10, 10), Monster(10));
// Error - No instance of overloaded function
如何将怪物实例放入怪物地图?我添加了所有运算符重载只是为了插入,但它不起作用!