0

I have the following code (stripped down for simplicity) which is using Aaron Liddiment's LED libraries:

#include <FastLED.h>
#include <LEDMatrix.h>
#include <LEDSprites.h>
#include <LEDText.h>

cLEDMatrix<16, 16, HORIZONTAL_ZIGZAG_MATRIX> leds;

class Class2{

  public:
    Class2(){
      init();
    };

    void init(){
      cLEDSprites Sprites(&leds);
    }

    bool loop(){
      Sprites.UpdateSprites();
      return true;
    }
};

I need to refer to the Sprites object in Class2::loop() as shown, but I'm told that 'Sprites' was not declared in this scope (which makes sense). If I move this line outside of the init function as follows:

class Class2{
  cLEDSprites Sprites(&leds);

  public:
    Class2(){};

    bool loop(){
      Sprites.UpdateSprites();
    }
};

I then get error: expected identifier before '&' token.

If Sprites was an int, I would declare a private attribute in Class2, pass the value into the class through the constructor and copy it over in the init function. I don't know how to do that with something of type cLEDSprites. As you can tell I'm quite new to all this stuff so please be gentle with my ignorance!

Scott Marley
  • 459
  • 1
  • 5
  • 8
  • 2
    The problem isn't the global variable `leds` but that you define the variable `Sprites` as a *local* variable inside the `init` function only. Perhaps it should be a member variable that you initialize with a *constructor initializer list*? Perhaps time to invest in [a good C++ book or two](https://stackoverflow.com/a/388282/440558)? – Some programmer dude Sep 14 '19 at 16:51
  • I want `Sprites` to be a local variable of `Class2`, not the `init` function. As I said in the question, if I move that line outside of the `init` function, I get the error message I wrote in the text which I don't really understand. You're right that I need to know a lot more about C++ though – Scott Marley Sep 14 '19 at 16:55
  • If you declare a variable inside any scope, it becomes a variable of that scope. So a variable declared inside *function* scope will be a local function variable. A variable declared in *class* scope will be class-scope variable (a.k.a. a member, field). – Yksisarvinen Sep 14 '19 at 17:22
  • That's what Some programmer dude said earlier, but when I try to declare it in class scope I get the error `error: expected identifier before '&' token`. I must be using the wrong syntax or something. I'll edit the question to make it clear that I understand this bit. – Scott Marley Sep 14 '19 at 17:25
  • 1
    You cannot initialize variable in class body (*probably*... At least I don't think Arduino supports C++11). That's what [member initializer list](https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) in constructor is for. – Yksisarvinen Sep 14 '19 at 17:27
  • Thanks for your link. I'm trying to get my head around it. It makes sense for `int`s but I just can't understand how to do it in my case. What should the constructor of `Class2` look like? Would I call the constructor with `Class2 class2 = Class2(&leds);`? – Scott Marley Sep 14 '19 at 17:56

1 Answers1

1

With member initilizer list syntax, your code may look like this:

#include <FastLED.h>
#include <LEDMatrix.h>
#include <LEDSprites.h>
#include <LEDText.h>

cLEDMatrix<16, 16, HORIZONTAL_ZIGZAG_MATRIX> leds;

class Class2{

  public:
    Class2():
            Sprites(&leds);
    {
    }

    bool loop(){
      Sprites.UpdateSprites();
      return true;
    }

  private:
    cLEDSprites Sprites;
};

Member initializer list can be used to initialize any class member, particularly those which cannot be default initialized (e.g. const members or classes without default constructor).
It's a good idea to initialize everything here - some people would even say that good constructor never has any lines in its body.

Of course, it would be better to not use global variables and just pass the pointer to the matrix to Class2 constructor, but I trust you will be able to do that on your own if you decide to not use global variables.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52