std::map<Item*, item_quantity_t> _items;
bool Inventory::hasItem(Item const& item) {
return (_items.find(&item) != _items.end() );
};
This code won’t work, but changing the input type of “hasItem” to Item & item will work…
can someone explain why to me? I’ve seen that std::find takes a const reference, so passing it a const object should be ok, at least it’s what I understand
You’ve defined that the key type for your map is a pointer to a non-const Item,
Item*. Thefindmethod expects a (const reference to a) value of the key type, so it requires a pointer to non-const.When you expand the templates, the parameter type of
findisItem* const&.You can either change your
hasItemto take a non-const referenceItem&, or you can change your key type to be a pointer to a const ItemItem const*.