Search code examples
cpebble-watchpebble-sdk

Error arising when assigning window .load and .unload functions


I'm working on a multi-window application and on my second window, I call this init() function

static struct MessageUI {
    Window *window;
    MenuLayer *menu_layer;
} ui;

...
...
...

void messages_init(void) {
    ui.window = window_create();

    window_set_window_handlers(ui.window, (WindowHandlers) {
        .load = window_load,
        .unload = window_unload
    });
}

When I run the code, I'm getting this error pertaining to the .load and .unload assignment operators.

../src/messages.c: In function 'messages_init':
../src/messages.c:63:3: error: initialization from incompatible pointer type [-Werror]
../src/messages.c:63:3: error: (near initialization for 'handlers.load') [-Werror]
../src/messages.c:65:2: error: initialization from incompatible pointer type [-Werror]
../src/messages.c:65:2: error: (near initialization for 'handlers.unload') [-Werror]

Any idea why this error is arising?

EDIT

Here are my window_load and window_unload functions

static void window_load(void) {
    // Create it - 12 is approx height of the top bar
    ui.menu_layer = menu_layer_create(GRect(0,0,144,168-16));
    menu_layer_set_click_config_onto_window(ui.menu_layer,ui.window);

    MenuLayerCallbacks callbacks = { 
        .draw_row = (MenuLayerDrawRowCallback) draw_row_callback,
        .get_num_rows = (MenuLayerGetNumberOfRowsInSectionsCallback) num_rows_callback,
        .select_click = (MenuLayerSelectCallback) select_click_callback
    };
    menu_layer_set_callbacks(ui.menu_layer, NULL, callbacks);

    //Add to window
    layer_add_child(window_get_root_layer(ui.window), menu_layer_get_layer(ui.menu_layer));
}

static void window_unload(void) {
    APP_LOG(APP_LOG_LEVEL_DEBUG, "unloading message UI");
}

Solution

  • That error happens because window_load and window_unload are both meant to take in a pointer to a Window. You should declare them like this:

    static void window_load(Window *window) {
        // ...
    }
    
    static void window_unload(Window *window) {
        // ...
    }