undo83
October 18, 2018, 11:54am
#1
Hi guys,
what i’m trying to do is:
I have a struct defined in this way:
struct menuItem{
short parent_id;
String title;
char* function;
}
Then i want to load items into the menu as so:
menuItem items = {{-1, “Home”, “showMenuHome”},{0, “Target points”, “showMenuTargetPoints”},{1, “New target point”, “editTP”}};
then i need a class where to define some functions… one of them should be:
void showTitles()
{
for(int i=0;i< sizeof items/sizeof items[0];i++)
Serial.println(items*.title);*
}
please help me out to do this…
struct menuItem
{
short parent_id;
String title;
char* function;
};
menuItem items[] =
{
{ -1, "Home", "showMenuHome"},
{0, "Target points", "showMenuTargetPoints"},
{1, "New target point", "editTP"}
};
void setup()
{
Serial.begin(115200);
showTitles();
}
void loop()
{
}
void showTitles()
{
for (int i = 0; i < sizeof items / sizeof items[0]; i++)
Serial.println(items[i].title);
}
undo83
October 18, 2018, 12:15pm
#3
Yes but what I am trying to do is to have showTitles inside a class... would that be possible?
undo83:
Yes but what I am trying to do is to have showTitles inside a class... would that be possible?
Absolutely, but your above code does not even try.
gfvalvo
October 18, 2018, 12:39pm
#5
Create a method in the class called 'showTitles'. Pass it a pointer or reference to an object whose class inherits from the 'Print' class. 'Serial' is one such object.
undo83
October 18, 2018, 1:45pm
#6
please can you show me some demo code for that? i've been trying for 2 days now to make it work and i can't seem to find the right way to do it...
gfvalvo
October 18, 2018, 2:28pm
#7
class myClass {
private:
uint8_t privateVariable;
public:
myClass (uint8_t p) : privateVariable(p) {}
void printPrivateVariable(Print *printPtr);
};
void myClass::printPrivateVariable(Print *printPtr) {
printPtr->print(privateVariable);
}
myClass myObject(4);
void setup() {
Serial.begin(115200);
Serial.println("Starting");
myObject.printPrivateVariable(&Serial);
Serial.println();
}
void loop()