使用此 Arduino 库的非静态成员函数
在Arduino项目(C ++)中,我使用 liquidmenu 库我很难将此课程整合到我自己的。对于实际情况,提供的示例太简单了。
某些未编译的代码:
main.cpp
const int rs = 3, en = 0, d4 = 4, d5 = 30, d6 = 12, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Menu menu(&lcd);
void setup() { }
void loop() { }
头
class Menu
{
public:
Menu(LiquidCrystal *lcd);
private:
int _myvar;
LiquidCrystal *_lcd;
LiquidMenu _menu;
LiquidScreen _screenSetCC1;
LiquidLine line1_setupCC1;
LiquidLine line2_setupCC1;
void editIntPlus();
};
CPP
Menu::Menu(LiquidCrystal *lcd) : _lcd(lcd), _menu(*_lcd),
line1_setupCC1(0, 0, "Set CC #1"),
line2_setupCC1(0, 1, _myvar)
{
line2_setupCC1.attach_function(1, editIntPlus);
_screenSetCC1.add_line(line1_setupCC1);
_screenSetCC1.add_line(line2_setupCC1);
_menu.add_screen(_screenSetCC1);
}
void Menu::editIntPlus()
{
_myvar++;
}
第一个错误是:
错误:非静态成员函数的使用无效'void菜单:: editintplus()'
但是我无法使该函数静态,否则我将无法再访问_MYVAR
。 示例违反任何编程的基本规则。
第二个问题。在我的菜单中,我要设置数十个int
var。 我真的不想创建很多功能来增加或减少每个var!
如何检索,edituintplus()
,当前var被编辑?目标是将相同的功能附加到所有VAR。
我看到此
- 无法声明
EditIntplus
为friend
:朋友: void EditIntplus();
导致:
include/menu.h:82:5: error: expected primary-expression before 'void'
void editIntPlus();
^~~~
- 如前所述,我不想使我的功能
static
否则我将无法再访问类成员
In an Arduino project (C++) I use the LiquidMenu library and I have difficulties to integrate this class in my own. The example provided are too simple for a real scenario.
Some code that does not compile:
main.cpp
const int rs = 3, en = 0, d4 = 4, d5 = 30, d6 = 12, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Menu menu(&lcd);
void setup() { }
void loop() { }
header
class Menu
{
public:
Menu(LiquidCrystal *lcd);
private:
int _myvar;
LiquidCrystal *_lcd;
LiquidMenu _menu;
LiquidScreen _screenSetCC1;
LiquidLine line1_setupCC1;
LiquidLine line2_setupCC1;
void editIntPlus();
};
cpp
Menu::Menu(LiquidCrystal *lcd) : _lcd(lcd), _menu(*_lcd),
line1_setupCC1(0, 0, "Set CC #1"),
line2_setupCC1(0, 1, _myvar)
{
line2_setupCC1.attach_function(1, editIntPlus);
_screenSetCC1.add_line(line1_setupCC1);
_screenSetCC1.add_line(line2_setupCC1);
_menu.add_screen(_screenSetCC1);
}
void Menu::editIntPlus()
{
_myvar++;
}
first error is:
error: invalid use of non-static member function 'void Menu::editIntPlus()'
But I cannot make that function static, otherwise I cannot access to _myvar
anymore.
The examples are not useful because they put everything outside any class and it's against any basic rule of programming.
Second question. In my menu I have dozens of int
vars to set.
I really don't want to create a lot of functions to increase or decrease each single var!
How to retrieve, inside editIntPlus()
, the current var being edited? The goal is to attach the same function to all the vars.
I saw this answer but:
I cannot declare
editIntPlus
asfriend
:friend:
void editIntPlus();
leads to:
include/menu.h:82:5: error: expected primary-expression before 'void'
void editIntPlus();
^~~~
- as said before, I don't want to make my function
static
otherwise I cannot access anymore to the class members
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据
user17732522
的建议,我分叉了库并进行了这些更改:LiquidMenu.h
LiquidLine.cpp
和我的代码(在
user17732522
建议后更正):Following the suggestion of
user17732522
I forked the library making these changes:LiquidMenu.h
LiquidLine.cpp
and in my code (corrected after
user17732522
suggestion):