WiringProject / Wiring

Wiring Framework
http://wiring.org.co/
Other
217 stars 168 forks source link

Errors creating instance MenuBackend and MenuItem attributes in a display class #49

Closed kamze closed 7 years ago

kamze commented 7 years ago

I am doing a display class using 2*8 lcd on an arduino to display and navigate my menu using MenuBackend library but i can make instance of these variables. I tried using static variable it did not work either.

Here is the code: GPSMenu.h :

#ifndef GPSMENU_h
#define GPSMENU_h
#include "Arduino.h"
#include <MenuBackend.h>
#include <LiquidCrystal.h>

class GPSMenu
{
public:
GPSMenu(LiquidCrystal* lcd);

  void navigateMenus( byte read);

  private:
  void menuChangeEvent(MenuChangeEvent changed);
  void menuUseEvent(MenuUseEvent used);
  void menuSetup();
  LiquidCrystal *_lcd;

  MenuBackend menu =  MenuBackend(menuUseEvent,menuChangeEvent); // these 2 parametter are functions

  //beneath is the list of menu items needed to build the menu
  MenuItem Battery     = MenuItem(menu, "Battery ", 1);//----usable
  MenuItem Iteneraire  = MenuItem(menu, "Itener  ", 1);
  MenuItem Start       = MenuItem(menu, "Start   ", 2);//----usable
  MenuItem Stop        = MenuItem(menu, "Stop    ", 2);//----usable
  MenuItem FileOpn     = MenuItem(menu, "FileOpn ", 2);
  MenuItem NewFile     = MenuItem(menu, "NewFile ", 3);//----usable
  MenuItem OverWr      = MenuItem(menu, "OverWr  ", 3);//----usable
  MenuItem PtOpn       = MenuItem(menu, "PtOpn   ", 2);
  MenuItem Seconde_1   = MenuItem(menu, "Court   ", 3);//----usable
  MenuItem Secondes_15 = MenuItem(menu, "Moyen   ", 3);//----usable
  MenuItem Minute_1    = MenuItem(menu, "Long    ", 3);//----usable
  MenuItem GPSData     = MenuItem(menu, "GPSData ", 1);
  MenuItem Coord       = MenuItem(menu, "Coord   ", 2);//----usable
  MenuItem Time        = MenuItem(menu, "Time    ", 2);//----usable
  MenuItem Status      = MenuItem(menu, "Status  ", 2);//----usable
  MenuItem SpdAlti     = MenuItem(menu, "SpdAlti ", 2);//----usable
  MenuItem Filemnger   = MenuItem(menu, "Filemngr", 1);//----usable
};

#endif

GPSMenu.cpp:

#include "Arduino.h"
#include <MenuBackend.h>    //MenuBackend library - copyright by Alexander Brevig
#include <LiquidCrystal.h>  //this library is included in the Arduino IDE
#include "GPSMenu.h"  //this library is included in the Arduino IDE

GPSMenu::GPSMenu(LiquidCrystal* lcd){
 _lcd = lcd;
 _lcd->begin(8, 2);
 _lcd->clear();
  GPSMenu::menuSetup();

}

//this function builds the menu and connects the correct items together
void GPSMenu::menuSetup()
{
  //add the file menu to the menu root
  menu.getRoot().add(Battery);
  Battery.addRight(Iteneraire).addRight(GPSData ).addRight(Filemnger);
  Iteneraire.add(Start ).addRight(Stop).addRight(FileOpn).addRight(PtOpn);
  GPSData.add(Coord).addRight(Time).addRight(Status).addRight(SpdAlti);
  FileOpn.add(NewFile).addRight(OverWr);
  PtOpn.add(Seconde_1).addRight(Secondes_15).addRight(Minute_1);
}

//  This is where you define a behaviour for a menu item

void GPSMenu::menuUseEvent(MenuUseEvent used)
 {
   _lcd->setCursor(0, 0);
   _lcd->print(used.item.getName());
   _lcd->setCursor(0, 1);
   _lcd->print("Used    ");
}
/*
  Here we get a notification whenever the user changes the menu
  That is, when the menu is navigated
*/
void GPSMenu::menuChangeEvent(MenuChangeEvent changed)
{
  _lcd->setCursor(0, 0);
  _lcd->print(changed.from.getName());
  _lcd->setCursor(0, 1);
  _lcd->print(changed.to.getName());
}

void GPSMenu::navigateMenus( byte read) {
  MenuItem currentMenu=menu.getCurrent();

  switch (read){

  case 1:

    if(!(currentMenu.hasAfterChildren())){  //if the current menu has a child and has been pressed enter then menu navigate to item below
      menu.use();
    }
    else{  //otherwise, if menu has no child and has been pressed enter the current menu is used
      menu.moveDown();
    }
    break;

  case 2:
    menu.moveLeft();

    break;
  case 3:
    menu.moveRight();

    break;
  case 4:
    menu.moveUp();
    break;
  }
}

Erros i am getting:

In file included from GPSMenu.cpp:4:0:
GPSMenu.h:20:46: error: 'void GPSMenu::menuUseEvent(MenuUseEvent)' cannot appear in a constant-expression
       static MenuBackend menu =  MenuBackend(menuUseEvent,menuChangeEvent);
                                              ^
GPSMenu.h:20:59: error: 'void GPSMenu::menuChangeEvent(MenuChangeEvent)' cannot appear in a constant-expression
       static MenuBackend menu =  MenuBackend(menuUseEvent,menuChangeEvent);
                                                           ^
GPSMenu.h:20:74: error: invalid in-class initialization of static data member of non-integral type 'MenuBackend'
       static MenuBackend menu =  MenuBackend(menuUseEvent,menuChangeEvent);
                                                                          ^
GPSMenu.h:20:74: error: initializer invalid for static member with constructor
GPSMenu.h:20:74: note: (an out of class initialization is required)
GPSMenu.h:23:46: error: 'GPSMenu::menu' cannot appear in a constant-expression
       static MenuItem Battery     = MenuItem(menu, "Battery ", 1);//----usable
                                              ^
GPSMenu.h:23:65: error: invalid in-class initialization of static data member of non-integral type 'MenuItem'
       static MenuItem Battery     = MenuItem(menu, "Battery ", 1);//----usable
                                                                 ^
GPSMenu.h:23:65: error: initializer invalid for static member with constructor
GPSMenu.h:24:58: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
       MenuItem Iteneraire  = MenuItem(menu, "Itener  ", 1);
                                                          ^
GPSMenu.h:25:58: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
       MenuItem Start       = MenuItem(menu, "Start   ", 2);//----usable

is it because of this line MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent); since i am calling menuUseEvent & menuChangeEvent here (if i do it without class it works just fine)?

AlexanderBrevig commented 7 years ago

Hi! You can't instantiate classes like that in the member listing of a class in C++.

Try just having MenuBackend menu; in the header, then instantiate it with an instantiation list in the constructor of your GPSMenu class something like: GPSMenu::GPSMenu(LiquidCrystal* lcd) : menu(menuUseEvent, menuChangeEvent) { and then declase menuUseEvent and menuChangeEvent as static void.

Contact me on alexanderbrevig ATSIGN gmail.com if you need further assistence :)

AlexanderBrevig commented 7 years ago

The email server had an error trying to email you so try this: GPSMenu.zip it's using the instantiation syntax for all your member classes.

kamze commented 7 years ago

Sorry it was my engineering school email : This is the same reply i sent you So i tried what u sent there are less errors there is only one remaining error which is repeated u=in several places:

In file included from GPSMenu.cpp:4:0: GPSMenu.h: In static member function ‘static void GPSMenu::menuUseEvent(MenuUseEvent)’: GPSMenu.h:37:20: error: invalid use of member ‘GPSMenu::_lcd’ in static member function LiquidCrystal _lcd; ^ GPSMenu.cpp:52:4: error: from this location _lcd->setCursor(0, 0); ^ In file included from GPSMenu.cpp:4:0: GPSMenu.h:37:20: error: invalid use of member ‘GPSMenu::_lcd’ in static member function LiquidCrystal _lcd; ^ GPSMenu.cpp:53:4: error: from this location _lcd->print(used.item.getName()); ^ In file included from GPSMenu.cpp:4:0: GPSMenu.h:37:20: error: invalid use of member ‘GPSMenu::_lcd’ in static member function LiquidCrystal *_lcd;

2017-06-24 13:06 GMT+02:00 Alexander Brevig notifications@github.com:

The email server had an error trying to email you so try this: GPSMenu.zip https://github.com/WiringProject/Wiring/files/1099609/GPSMenu.zip it's using the instantiation syntax for all your member classes.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/WiringProject/Wiring/issues/49#issuecomment-310832229, or mute the thread https://github.com/notifications/unsubscribe-auth/ALmlsGC-O0NRKfa1T01N-FaGu3YYHg-Jks5sHO3FgaJpZM4OECOC .