troopy28 troopy28 - 1 month ago 9
C++ Question

C++ - std::map with an enum as key

I'm currently trying to create an std::map to store the state of each key of the keyboard. I've created the following map to do it :

static std::map<enum MouseCode, enum InputState> mousePressedMap;


No problem here.

To register the states, I've created a function called by my main class :

void Input::ReadUserInput(enum Input::MouseCode mouseCode, enum Input::InputState inputState) {
mousePressedMap[mouseCode] = inputState;
}


And here I'm getting my problem : I got an error on the "[" of the mousePressedMap saying : no "[]" operator corresponds to those operands.
And I cannot either do :

void Input::ReadUserInput(enum Input::MouseCode mouseCode, enum Input::InputState inputState) {
mousePressedMap[MouseCode::LeftButton] = InputState::DOWN;
}


I got the same error.

Besides, I think the error doesn't come from my enumerations :

/// <summary>
/// <para>Enumeration to set parameters to the mouse buttons handling function. The mouse code defines which button has been called.</para>
/// </summary>
enum MouseCode {
LeftButton = 0,
MiddleButton = 1,
RightButton = 2
};
/// <summary>
/// <para>Enumeration to set parameters to the mouse buttons handling function. The input state is the current state of the button</para>
/// </summary>
enum InputState {
/// <summary>
/// Just pressed.
/// </summary>
DOWN,
/// <summary>
/// Just released.
/// </summary>
UP,
/// <summary>
/// Was already pressed the last frame.
/// </summary>
PRESSED,
/// <summary>
/// Is not currently activated and wasn't the last frame.
/// </summary>
INACTIVE
};


Can someone explain me why? I come from C# and Java languages where this syntax was perfectly working and it's quite strange that is not the case here.

Thanks for your help!

EDIT 1 :
The error message is (in French, sorry...):
Erreur C2679 '[' binaire : aucun opérateur trouvé qui accepte un opérande de partie droite de type 'InputsManagement::Input::MouseCode' (ou il n'existe pas de conversion acceptable) Win7 DirectX c:\users\maxime\desktop\directx tests\win7 directx\win7 directx\input.cpp 16

Answer

So, the purpose of the code is to map the values 0, 1, and 2 into other values? Use an array, indexed by the MouseCode values.