I'm trying to make a class to expand on the functionality of the Adafruit-RGB-LCD-Shield-Library I'm creating this based on what I see from this post.
class LcdMenu
{
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
public:
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
void Setup()
{
lcd.begin(16, 2);
}
void Write(int line, char text[17])
{
lcd.clear();
}
};
LcdMenu *LcdM;
#define VIOLET 0x5
void setup()
{
LcdM->Setup();
LcdM->lcd.setBacklight(VIOLET);
}
However, I'm getting the following error:
C:\Users\jschere2\AppData\Local\Temp\ccUzvCmX.ltrans0.ltrans.o: In function `main':
ccUzvCmX.ltrans0.o:(.text.startup+0xf0): undefined reference to `LcdMenu::Adafruit_RGBLCDShield::begin(unsigned char, unsigned char, unsigned char)'
ccUzvCmX.ltrans0.o:(.text.startup+0xf8): undefined reference to `LcdMenu::Adafruit_RGBLCDShield::setBacklight(unsigned char)'
collect2.exe: error: ld returned 1 exit status
exit status 1 Error compiling for board Arduino/Genuino Mega or Mega 2560.
1 Answer 1
Move your #include
statements outside the class definition.
You declare a pointer, but never allocate space for it with new
. I see no need for a pointer.
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
class LcdMenu
{
public:
// default ctor will be called here.
Adafruit_RGBLCDShield lcd;
void Setup()
{
lcd.begin(16, 2);
}
void Write(int line, char text[17])
{
lcd.clear();
}
};
LcdMenu LcdM;
#define VIOLET 0x5
void setup()
{
LcdM.Setup();
LcdM.lcd.setBacklight(VIOLET);
}
Also note: you are not extending the Adafruit_RGBLCDShield
class as in the example you linked. To do that:
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
class LcdMenu : public Adafruit_RGBLCDShield
{
public:
void Setup()
{
begin(16, 2);
}
void Write(int line, char text[17])
{
clear();
}
};
LcdMenu LcdM;
#define VIOLET 0x5
void setup()
{
LcdM.Setup();
LcdM.setBacklight(VIOLET);
}
-
Ok, in my larger program should I allocate space for the pointer? What is the advantage of having a pointer in a larger program?ATE-ENGE– ATE-ENGE2017年07月20日 15:23:16 +00:00Commented Jul 20, 2017 at 15:23
-
1The advantage to using pointers is to create objects on-the-fly. For example, create an object, use it, then free the memory associated with it. (There are other uses for pointer. This is just one example). If the object exists for the life of the program, a pointer is not needed.001– 0012017年07月20日 15:25:12 +00:00Commented Jul 20, 2017 at 15:25
-
Ok, What metric should I use to determine if I should extend the class or keep it separate?ATE-ENGE– ATE-ENGE2017年07月20日 15:36:04 +00:00Commented Jul 20, 2017 at 15:36
-
1You could use the "has-a/is-a" concept. Is your class an LCD ("is a") or does it just use one ("has a")? Here's an SO question that discusses it: HAS-A, IS-A terminology in object oriented language001– 0012017年07月20日 15:39:29 +00:00Commented Jul 20, 2017 at 15:39
-
Ok and I should extend the class if
LcdMenu
is a(n)Adafruit_RGBLCDShield
?ATE-ENGE– ATE-ENGE2017年07月20日 16:00:13 +00:00Commented Jul 20, 2017 at 16:00