1
votes

I'm having 2 enums which are of the newer enum class type.

enum class Action
{
    Move,
    Attack,
    Die,
    Splash,
    Idle
};

enum class Facing
{
    Left,
    LeftUp,
    LeftDown,
    Up,
    Down,
    Right,
    RightUp,
    RightDown
};

And I want to store this stuff inside a multimap:

std::multimap<Entity::Facing,std::pair<Entity::Action,std::unique_ptr<Animation>>> listAnimation;

The key is: facing and the pair is the action of the entity + the animation.

This is how i insert it:

std::unique_ptr<Animation> splashUp (new Animation());
splashUp->setSpriteSheet(*texture);
splashUp->addFrame(sf::IntRect(3584,256,128,128));
splashUp->addFrame(sf::IntRect(3712,256,128,128));
splashUp->addFrame(sf::IntRect(3840,256,128,128));
splashUp->addFrame(sf::IntRect(3968,256,128,128));
splashUp->addFrame(sf::IntRect(4096,256,128,128));
splashUp->addFrame(sf::IntRect(4224,256,128,128));
splashUp->addFrame(sf::IntRect(4352,256,128,128));
splashUp->addFrame(sf::IntRect(4480,256,128,128));

this->listAnimation.insert(Entity::Facing::Up, std::make_pair(Entity::Action::Splash, std::move(splashUp)));

and this is the error which I can't wrap my head around even after alot of googling:

error C2664: 'std::_Tree_iterator<_Mytree> std::multimap<_Kty,_Ty>::insert(std::_Tree_const_iterator<_Mytree>,const std::pair<_Ty1,_Ty2> &)' : cannot convert parameter 1 from 'Entity::Facing' to 'std::_Tree_const_iterator<_Mytree>' 1>
with 1> [ 1>
_Mytree=std::_Tree_val>>>>, 1> _Kty=Entity::Facing, 1>
_Ty=std::pair>, 1> _Ty1=const Entity::Facing, 1> _Ty2=std::pair> 1> ] 1> and 1> [ 1>
_Mytree=std::_Tree_val>>>> 1> ] 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Can I use enum class as Key in multimap at all?

2
which insert overload allows you to do what you want? Read documentation and check how insert works. - Jepessen
You may use a std::unordered_multimap. std::multimap requires a definition of the std::less() comparator for the key type. - πάντα ῥεῖ
@πάντα ῥεῖ yes that was the container i was looking for. Trying to figure out how to implement a hash now for my enum class. Cheers mate - user2946316

2 Answers

1
votes

multimap<Key, Value>::insert() takes only one parameter, which should be convertible to std::pair<const Key, Value>.

For convenience, and possibly for some speedup (because you don't have to create a temporary pair), you can use emplace() instead:

listAnimation.emplace(Entity::Facing::Up, std::make_pair(Entity::Action::Splash, std::move(splashUp)));
0
votes

If you check the docs for multimap::insert, you can see that there is no method with a signature that matches what you're trying to do. The methods are:

single element (1) iterator insert (const value_type& val); with hint (2) iterator insert (iterator position, const value_type& val); range (3) template void insert (InputIterator first, InputIterator last);`

You are attempting to insert a value into the object, i.e. (1), but the compiler is confused, as the function arity makes it look like the latter 2.

For inserting a value, you need to create a pair, i.e., insert(make_pair(...)). As a special case, each data is mapped in your case to a pair as well, so it will end up being insert(make_pair(..., make_pair(...))).

In any case, there is no insert that takes a key and a mapping as two parameters, as you are trying to do.