0

I have a class:

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;}
};

Some variables (like canmove and notforward) are inherited. Next, in a different class, I create a map of monsters:

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

How can I get the Monster instances into the monsters map? I added all the operator overloads just so I could insert, but it doesn't work!

Brian Gradin
  • 2,165
  • 1
  • 21
  • 42

1 Answers1

0

Simple way is

monsters[make_pair(10, 10)] = Monster(10);

another way is

monsters.insert(make_pair(make_pair(10, 10), Monster(10)));

yet another is

monsters.insert(map<pair<int, int>, Monster>::value_type(make_pair(10, 10), Monster(10)));

Operator overloads are unnecessary, you do need to overload operator< for the key of a map but not for the value. I think maybe you got confused because two calls to make_pair are necessary in the second case.

john
  • 85,011
  • 4
  • 57
  • 81