Referencing Keyboard issues [RESOLVED]

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
YoMomIsANiceLady
Posts: 33
Joined: February 2nd, 2017, 8:20 am

Referencing Keyboard issues [RESOLVED]

Post by YoMomIsANiceLady » February 16th, 2017, 7:36 pm

NEVER MIND! I resolved the issue as I was typing this post (Classic rookie mistake). But I might keep it in here just in case someone is having a similar problem.

___________________________________________________________

PROBLEM:

I'm having this issue with the Keyboard class and I'm not quite sure why. Can somebody explain what is going on?

Basically I have created a Menu class for the Snake Game and I have a function called navigate(). That uses the keyboard input to navigate through the menu.

If I make the navigate function take a Keyboard parameter like this:

Code: Select all

void Menu::navigate(Keyboard &kbd)
{
    int option = -1;
    while ( !kbd.KeyIsEmpty() ) {         	
        const Keyboard::Event e = kbd.ReadKey();
        if (e.IsRelease()) {
            if (e.GetCode() == VK_UP || e.GetCode() == VK_DOWN || e.GetCode() == 0x53 || e.GetCode() == 0x57) {
                buttonPressed = false;
            }             	
        }
   ...

everything works fine. But if I make the function take no parameters and instead have the Keyboard be a member variable, like this:

Menu.h:

Code: Select all

Keyboard &kbd;


It compiles fine but throws an error at runtime: "Couldn't read from memory location 0xFFFFFFFFF (not sure how many F's)



_______________________________________________________________________


SOLUTION:

The issue was that I was not actually sending the Keyboard reference from Game.cpp
So I had to add kbd(kbd) to the constructor's initializing list:

Menu.cpp:

Code: Select all

Menu::Menu(Keyboard &kbd) {
       :
    kbd(kbd)
}
And menu(kbd) to the Game's initializer list

Code: Select all

Game::Game(MainWindow& wnd)
	:
    menu(wnd.kbd),
"Life is like death, but completely different"
- Ivan Gašparovič

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Referencing Keyboard issues [RESOLVED]

Post by chili » February 17th, 2017, 1:14 am

I don't understand how that even compiled XD
Chili

User avatar
YoMomIsANiceLady
Posts: 33
Joined: February 2nd, 2017, 8:20 am

Re: Referencing Keyboard issues [RESOLVED]

Post by YoMomIsANiceLady » February 17th, 2017, 2:05 am

Yeah neither do I. I stopped when I was typing "it compiled fine" I was like.. "Are you sure it did?"... and yeah, it definitely did, it was strange
"Life is like death, but completely different"
- Ivan Gašparovič

Post Reply