An object as argument to a function in the library?

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;
                     ^~~~~

This error message (especially the line number) leads me to believe that the project you are trying to compile is not equal to what you have shared in your post.

Thanks for replying. And indeed, the full project is a bit larger. But following the guidelines to this forum I condensed the code to the absolute minimum required for replicating the error message. Consequently, line numbers are not telling ...:wink:

The code you posted will not produce that error.

Thanks a lot, gfvalvo. you made my day!!
My embarassing error: In the library cpp file i had forgotten the reference to the class (MYLIB::slight_smile: in front of the function definition. Instead, I blamed the missing 'declaration' of the type of object ('displ') in the h-file for the error message. That seems to be superfluous anyway (??? for the C++ newbie). However, case closed thanks to your endeavours. Have a good day!

It's saying that the problem is in Line 78 of Arduino/libraries/MYLIB/myLib.h but the 'myLib.h' you are showing doesn't seem to be more than 78 lines long.

hi johnwassser, thanks for your fast response. you are right, as was jfjlaros, making the same observation (see my response to him). meanwhile the problem is solved (see my comment to gfvalvo). while still feeling uncomfortable with the solution (blind flight mode does not offer comfort), it's the best I could hope for as a newbie to Arduino/C++.

So what is your solution? Without the whole code we can’t tell you if you picked the right way to do things.

(And I’m not sure why you wanted to make a class for this function)

...the solution is to take the code given in my first post, because it turned out, that it works. See my reply to gfvalvo, who was so kind to actually test this code.

Why a class? For no better reason than seeing others working with classes successfully whenever it comes to organize reusable code in libraries. Of course, If I stumble over sufficient code doing without classes, I might revise this decision.

may be you had some use for these in the real class and code you did not show?

if not, creating an instance just to get the member function has limited interest. You could just structure the code so that there are other files just with helpers functions, they don't need to be in a class - it's not JAVA :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.