0

This is probably a really simple thing but I'm new to C++ so need help.

I am trying to make a simple snake game, written in C++ and meant to run on a MBED MCU. In order to keep the code as tidy as possible, I have created a class to store all the bitmaps (i.e. 2D arrays of 1s and 0s) that will be printed as settings and interaction icons on the Menu screen of th game.

The header file (Menu.h) looks like this:

#ifndef MENU_H
#define MENU_H

#include "mbed.h"
#include "N5110.h" //lcd screen
#include "Gamepad.h" //board on which the MBED is mounted, with leds etc.

class Menu {

public:
     Menu();
    ~Menu();
    static const char snake_title[24][70]; //This is the first of several    
// arrays that I want to include in the struct
    void init_array();
    void welcome(N5110 &lcd, Gamepad &pad);

private:

};

#endif

And this is the implementation file (Menu.cpp)

#include "Menu.h"

Menu::Menu()
{

}

Menu::~Menu()
{

}

void Menu::init_array()
{
    const char snake_title[24][70] = {
    //Massive array of bitmaps e.g. {0,0,0,0,0,0,1,1,0,1,1,1,1.....0,0,0},
    ......
    ......
    ......
};
}

void Menu::welcome(N5110 &lcd, Gamepad &pad)
{

    lcd.drawSprite(7,12,24,70,(int *)snake_title); //draw sprite, position 68,12
    lcd.printString("Press Start",10,5);
    lcd.refresh();

    // wait flashing LEDs until start button is pressed
    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
    }


}

I haven't tried to make a struct to encapsulate all the 2D arrays yet because at this stage the code is already not working, and I would like to know why and what I can do to fix it.

The two errors I get make no sense to me:

Error: "/tmp/CLhdO7", line 124 (column 3): Warning: L6312W: Empty Execution region description for region RW_IRAM1

Error: Undefined symbol Menu::snake_title (referred from /build/Menu/Menu.K64F.o).

Thanks for your help!

1 Answers1

0

When you declared your static const char snake_title[24][70]; member, you didn't provide a definition for it.

The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator. The initializer expression in the definition of a static data member is in the scope of its class

Attempting to declare and define a variable with the same name inside void Menu::init_array() has no relation to your member variable. What you need is to get rid of init_array() all together and instead define your snake_title in Menu.cpp like this:

const char Menu::snake_title[24][70] = {
    //Massive array of bitmaps e.g. {0,0,0,0,0,0,1,1,0,1,1,1,1.....0,0,0},
    ......
    ......
    ......
};
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37
  • Thanks so much, that compiles! What if I were to call the welcome function in the main file? I am trying to but without success, I want the "sprite" resulting from the bitmap to be printed to the lcd screen. – Andrea Loriedo Mar 19 '18 at 23:25