I’m working on an input class for a sample C++ project by following some online tutorials, and currently stuck in a weird problem.
In my header class, I have a public child class called KeyboardEvent
class KeyboardEvent
{
public:
enum class KeyboardEventType
{
Invalid,
Press,
Release
};
private:
KeyboardEventType eventType;
unsigned char keycode;
public:
KeyboardEvent(KeyboardEventType type, unsigned char code) :
eventType(type),
keycode(code)
{}
bool IsPress()
{
return eventType == KeyboardEventType::Press;
}
bool IsRelease()
{
return eventType == KeyboardEventType::Release;
}
char GetKeyCode()
{
return keycode;
}
};
Along with that, I have these 3 private variables in the same header:
`static constexpr unsigned int KeysLength = 256u;
std::bitset<KeysLength> keyStates;
std::queue<KeyboardEvent> keyBuffer;`
Now if I call this method below which is in my .cpp file, I get an access violation error on both, keystates[keyCode] as well as keyBuffer.push if I put that on top.
void KeyboardInput::OnKeyPressed(unsigned char keyCode) noexcept
{
keyStates[keyCode] = true;
keyBuffer.push(KeyboardInput::KeyboardEvent
(
KeyboardInput::KeyboardEvent::KeyboardEventType::Press, keyCode
));
TrimBuffer(keyBuffer);
}
I’ve tried changing the C++ standard, but that would mean my entire project might stop working. I have also tried with and without “noexcept”, but to no avail. The tutorials I am following have the exact code, but just mine doesn’t work. Ideally it should just assign a value to the keyCode index in keyStates and push a new KeyboardEvent to the keyBuffer queue.
But this is unfortunately how my keyStates and keyBuffer look like on breakpoints
enter image description here
enter image description here
Any help? Thank you!
Painkiller Cyborg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.