Hi there,
I'm trying to write a function for printing text to a sprite, where sprite's name is given as argument to the function. This works fine as in
// ===================================================================
// singlefile.ino
// ===================================================================
#include <M5Core2.h>
TFT_eSprite sprite1(&M5.Lcd);
TFT_eSprite sprite2(&M5.Lcd);
void printToSprite(TFT_eSprite &displ, const char* sometext) {
displ.println(sometext);
displ.pushSprite(0,0);
}
void setup() {
M5.begin();
sprite1.createSprite(320,240);
sprite2.createSprite(320,240);
printToSprite(sprite1,"some text to sprite 1");
printToSprite(sprite2,"some other text to sprite 2");
}
void loop() {
}
But matters go wrong when I move the relevant material into my library and try to call the function from the library. Code of ino-sketch-, h- and cpp-file are given below as are compiler error messages (verbose output being turned on).
Any hint as of how to correct this code is highly appreciated.
Thanks, didigs
// ===================================================================
// somesketch.ino
// ===================================================================
#include <M5Core2.h>
#include <mylib.h>
MYLIB showinfo;
TFT_eSprite sprite1(&M5.Lcd);
TFT_eSprite sprite2(&M5.Lcd);
void setup() {
M5.begin();
sprite1.createSprite(320,240);
sprite2.createSprite(320,240);
showinfo.printToSprite(sprite1,"some text to sprite 1");
showinfo.printToSprite(sprite2,"some other text to sprite 2");
}
void loop() {
}
// ===================================================================
// mylib.h
// ===================================================================
#ifndef MYLIB_H
#define MYLIB_H
#include <M5Core2.h>
class MYLIB
{
public:
MYLIB();
void printToSprite(TFT_eSprite &displ,const char* sometext);
private:
int X;
int Y;
int dX;
const char* sometext;
};
#endif
// ===================================================================
// mylib.cpp
// ===================================================================
#include <M5Core2.h>
#include <mylib.h>
MYLIB::MYLIB() {}
void MYLIB::printToSprite(TFT_eSprite &displ,const char* sometext) {
displ.println(sometext);
displ.pushSprite(0,0);
}
// ===================================================================
// error messages when compiling somesketch.ino
// ===================================================================
Arduino: 1.8.19 (Linux), Board: "M5Stack-Core2, Enabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 16MB (128Mb), 460800, Core 1, Core 1, Debug"
~/Arduino/libraries/MYLIB/mylib.cpp: In constructor 'MYLIB::MYLIB()':
~/Arduino/libraries/MYLIB/mylib.cpp:13:1: error: uninitialized reference member in 'class TFT_eSprite&' [-fpermissive]
MYLIB::MYLIB() {}
^~~~~
In file included from ~/Arduino/libraries/MYLIB/mylib.cpp:8:
~/Arduino/libraries/MYLIB/mylib.h:78:21: note: 'TFT_eSprite& MYLIB::displ' should be initialized
TFT_eSprite &displ;
^~~~~