vendredi 3 juillet 2015

c++ unordered_multimap insert hash

I'm going crazy over here. I have search google to find 1 single decent example where people use a unordered_map together with enum class and a hash function without any luck. Those i manage to find always end up saying "use map instead".

I'm trying to do the following:

Enum class facing is the direction my sprite is looking at.

Enum class Action is the action my sprite is doing.

Animation is a class that holds different animations which i will call later.

The container should look like this:

map

There can be more than 1 FACING in the map as key and there can be more than one ACTION in the pair.

Example:

map<LEFT, pair<ATTACK, attackAnimation>
map<LEFT, pair<IDLE, idleAnimation>
map<LEFTUP, pair<IDLE, idleAnimation>

This is an simplified everything

#include <iostream>
#include <unordered_map>
#include <string>
#include <memory>

template <typename T>
struct Hash
{
    typedef typename std::underlying_type<T>::type underlyingType;
    typedef typename std::hash<underlyingType>::result_type resultType;
    resultType operator()(const T& arg) const
    {
        std::hash<underlyingType> hasher;
        return hasher(static_cast<underlyingType>(arg));
    }
};

class Animation
{
private:
    std::string str;

public:
    Animation(std::string _string)
    {
        this->str = _string;
    }

    std::string& GetString()
    {
        return this->str;
    }
};

class Bullshit
{
public:
    enum class Action
    {
        Attack,
        Move
    };

    enum class Facing
    {
        Right,
        Up,
        Left
    };

    Bullshit()
    {

    }

    std::unordered_multimap<Bullshit::Facing, std::pair<Bullshit::Action, std::unique_ptr<Animation>>,Hash<Bullshit::Facing>>& GetlistAnimation()
    {
        return this->listAnimation;
    }

private:
    std::unordered_multimap<Bullshit::Facing, std::pair<Bullshit::Action, std::unique_ptr<Animation>>,Hash<Bullshit::Facing>> listAnimation;
};


int main()
{
    Bullshit bull;
    auto myList = bull.GetlistAnimation();

    std::unique_ptr<Animation> anim(new Animation("test"));
    myList.insert(std::make_pair(Bullshit::Facing::Up, std::make_pair(Bullshit::Action::Attack, std::move(anim))));

    std::cin.get();
    return 0;
}

Error code:

error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1>          with
1>          [
1>              _Ty=Animation
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(1447) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
1>          with
1>          [
1>              _Ty=Animation
1>          ]
1>          This diagnostic occurred in the compiler generated function 'std::pair<_Ty1,_Ty2>::pair(const std::pair<_Ty1,_Ty2> &)'
1>          with
1>          [
1>              _Ty1=Bullshit::Action,
1>              _Ty2=std::unique_ptr<Animation>
1>          ]

Aucun commentaire:

Enregistrer un commentaire