Error trying to use a static function in a class outside

what am i doing wrong here?

sketch (note line 62)

//====================================================================================================================== //
// MY FASTLED
//====================================================================================================================== //
  #ifndef MYFASTLED_H
  #define MYFASTLED_H
 
  #define FASTLED_ESP8266_RAW_PIN_ORDER
  #include <FastLED.h>
 
  #define NUM_LEDS_PER_STRIP 199             // NUM LEDS PER STRIP || Studio Lights = 199 | Neon heaven = 32 | Vitrineskap = 28
  #define NUM_STRIP 1                        // NUM STRIPS (NS)
  #define COLOR_ORDER GRB                    // COLOR ORDER
  #define LED_TYPE WS2812B                   // LED TYPE
  #define FRAMES_PER_SECOND  120             // FRAMES PER SECOND (FPS)
  #define DATA_PIN 15                        // LED PIN || Studio Lights = 15 / D8 | Neon heaven = 14 / D5 | Vitrineskap = 00 / D8
 
   
  CRGB Strip[NUM_STRIP][NUM_LEDS_PER_STRIP];
 
//-----------------------------------------------------------------------------------------------------------------------//
  class MYFASTLED {
   
    private:
 
      enum direction { FORWARD, REVERSE };
      static int LEDBrightness;
 
      typedef struct {    
        int  ActivePattern;        // which pattern is running
        direction Direction;       // direction to run the pattern
        int TotalSteps;            // total number of steps in the pattern
        uint16_t Index;            // current step within the pattern
        unsigned long lastUpdate;  // last update of position
        byte Hue, Sat, Val;        // HSV Color
        int Delay;                 // User Delay
        int Pattpos;               // Current Pattern Position
        int Spacing;               // Spacing between LEDs (in some patterns)
        uint16_t Delta;            // Delta in patterns
        uint16_t Pos;              // Position in pattern
      } record_type;
 
      static record_type ARRAY_STRIP_SETTING[NUM_STRIP]; // Make Array of the struct (SSET)
    
// FUNCTIONS
//-----------------------------------------------------------------------------------------------------------------------// 
  static void SETUP() {
    FastLED.addLeds<WS2812B, DATA_PIN, GRB>(Strip[0], NUM_LEDS_PER_STRIP).setCorrection(TypicalLEDStrip);
  }
//-----------------------------------------------------------------------------------------------------------------------// 
  static void Increment(int i) {    
    if (ARRAY_STRIP_SETTING[i].Direction == FORWARD) {
        ARRAY_STRIP_SETTING[i].Index++;
    if (ARRAY_STRIP_SETTING[i].Index >= ARRAY_STRIP_SETTING[i].TotalSteps) { ARRAY_STRIP_SETTING[i].Index = 0; }
    } else { // Direction == REVERSE
      --ARRAY_STRIP_SETTING[i].Index;
    if (ARRAY_STRIP_SETTING[i].Index <= 0) { ARRAY_STRIP_SETTING[i].Index = ARRAY_STRIP_SETTING[i].TotalSteps - 1; }
    }
  }
//-----------------------------------------------------------------------------------------------------------------------// 
    public:
    
      static void SetLEDBrightness (int Brightness) { LEDBrightness = Brightness; }
 
  };

  int MYFASTLED::LEDBrightness = 0;
 
  #endif

then in my other file im including this class and trying to do this

MYFASTLED::SetLEDBrightness(0);

Error

c:/users/mythical force/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/2.5.0-3-20ed2b9/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: sketch\Main_Code.ino.cpp.o:C:\Users\Mythical Force\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Serialization/serialize.hpp:23: undefined reference to `MYFASTLED::LEDBrightness'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

what am i doing wrong here?

You're pasting code links instead of posting code inline in code tags, which is the forum protocol. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...

aarg:
You’re pasting code links instead of posting code inline in code tags, which is the forum protocol. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question …

fixed!

You are using a static static member function that means it can only access static data member, other static member functions and any other functions from outside the class. Here you are trying to access the static data member LEDBrightness, the function call is fine but the space for the data itself is not allocated.

you need to add after your class definitionint MYFASTLED::LEDBrightness = 0; // or whatever default value you want for this class variableThat will truly allocate the variable and make it known to the compiler. (Default value optional if initialized at 0 as static are initialized to 0 by the compiler).

Then your function call should be all fine.

J-M-L:
You are using a static static member function that means it can only access static data member, other static member functions and any other functions from outside the class. Here you are trying to access the static data member LEDBrightness, the function call is fine but the space for the data itself is not allocated.

you need to add after your class definitionint MYFASTLED::LEDBrightness = 0; // or whatever default value you want for this class variableThat will truly allocate the variable and make it known to the compiler. (Default value optional if initialized at 0 as static are initialized to 0 by the compiler).

Then your function call should be all fine.

so that worked, thanks for that! :slight_smile: was trying todo the same for ARRAY_STRIP_SETTING

like this

int MYFASTLED::ARRAY_STRIP_SETTING[0].ActivePattern = 1;

but then i get

MyFastLED.h:175:40: error: expected initializer before '.' token

   int MYFASTLED::ARRAY_STRIP_SETTING[0].ActivePattern = 1;

                                        ^

exit status 1
expected initializer before '.' token

Same thing, you have to declare the ARRAY_STRIP_SETTING array first with the right type and number of elements. Then in the function you'll be able to access the first element of the array and its attributes so probably more something like thisrecord_type MYFASTLED::ARRAY_STRIP_SETTING[NUM_STRIP];assuming record_type and NUM_STRIP are known

It looks like you are confused about object oriented programing. Why do you need an array of struct being static and have no instance variables in your class ? you would be better off having your struct's elements as the instance variables of your class and then have your main code handle an array of instances. They could all share the same brightness if you define it as a class variable.

J-M-L:
Same thing, you have to declare the ARRAY_STRIP_SETTING array first with the right type and number of elements. Then in the function you'll be able to access the first element of the array and its attributes so probably more something like thisrecord_type MYFASTLED::ARRAY_STRIP_SETTING[NUM_STRIP];assuming record_type and NUM_STRIP are known

It looks like you are confused about object oriented programing. Why do you need an array of struct being static and have no instance variables in your class ? you would be better off having your struct's elements as the instance variables of your class and then have your main code handle an array of instances. They could all share the same brightness if you define it as a class variable.

yea im still learning about class and objects!

there is no need for them to share brightness! as the FastLED library has a function "FastLED.setBrightness(int)" that controll brightness over the entire LED strip.

so why would you want anything to be static then ?

J-M-L:
so why would you want anything to be static then ?

there is no reason i've just looked at some tutorials on how to write a class in arduino and well that's how it was done.

also someone said to me

Or alternately you could add static to the ARRAY_STRIP_SETTING declaration so it belongs to the class itself, since this is sort of a singleton instance pattern anyway.

when you use static for a data element it means there will be only one copy of it, shared by all the instances of your class. if that's not what you intend to do and want each instance to have their own data, then don't use static.

J-M-L:
when you use static for a data element it means there will be only one copy of it, shared by all the instances of your class. if that's not what you intend to do and want each instance to have their own data, then don't use static.

thanks for that info.

All i want is to access the public functions that sets the variables inside the class outside of the class

      static void SetLEDBrightness (int TempBrightness) { LEDBrightness = TempBrightness; }
      static void SetActivePattern (int Pat) { ARRAY_STRIP_SETTING[0].ActivePattern = Pat; }

so that i could easily just call

 MYFASTLED::SetActivePattern(int);

to set the pattern that i want

so i would be better of just not using static here then?

why don’t you just create something like this

class MYFASTLED {
  private:
    enum t_direction { FORWARD, REVERSE };

    int LEDBrightness;
    int  ActivePattern;        // which pattern is running
    t_direction Direction;       // direction to run the pattern
    int TotalSteps;            // total number of steps in the pattern
    uint16_t Index;            // current step within the pattern
    unsigned long lastUpdate;  // last update of position
    byte Hue, Sat, Val;        // HSV Color
    int Delay;                 // User Delay
    int Pattpos;               // Current Pattern Position
    int Spacing;               // Spacing between LEDs (in some patterns)
    uint16_t Delta;            // Delta in patterns
    uint16_t Pos;              // Position in pattern
    
  public:
    void SetLEDBrightness (int b) {
      LEDBrightness = b;
    }

    void SetActivePattern (int p) {
      ActivePattern = p;
    }

....
};

J-M-L:
why don't you just create something like this

class MYFASTLED {

private:
   enum t_direction { FORWARD, REVERSE };

int LEDBrightness;
   int  ActivePattern;        // which pattern is running
   t_direction Direction;       // direction to run the pattern
   int TotalSteps;            // total number of steps in the pattern
   uint16_t Index;            // current step within the pattern
   unsigned long lastUpdate;  // last update of position
   byte Hue, Sat, Val;        // HSV Color
   int Delay;                 // User Delay
   int Pattpos;               // Current Pattern Position
   int Spacing;               // Spacing between LEDs (in some patterns)
   uint16_t Delta;            // Delta in patterns
   uint16_t Pos;              // Position in pattern
   
 public:
   void SetLEDBrightness (int b) {
     LEDBrightness = b;
   }

void SetActivePattern (int p) {
     ActivePattern = p;
   }

....
};

Oh im sorry i forgot to mention THAT i need the struct array because! i have more then 1 strip connected to my ESP so each strip has its own settings, Activepattern, delay, hue and all that :slight_smile: that's why i have a struct array! the only thing as mentioned was the LEDBrightness! that is overall controll on all strips so that dont need to be in a array or struct :slight_smile:

well that’s the whole point of a class… you’ll instantiate as many MYFASTLED as you need… one per strip… and your main code then has an array of instances of MYFASTLED

if you want something to gather all those instances and handle the scheduling etc then that could be another class, which would own an array of MYFASTLED instances;

J-M-L:
well that’s the whole point of a class… you’ll instantiate as many MYFASTLED as you need… one per strip… and your main code then has an array of instances of MYFASTLED

if you want something to gather all those instances and handle the scheduling etc then that could be another class, which would own an array of MYFASTLED instances;

it sounds very nice but we are way out of my coding experience right there :stuck_out_tongue:

btw i removed all static(ness) of the class and now everything works perfectly :slight_smile: i can call functions by doing

MYFASTLED MYFASTLED;


MYFASTLED.setBrightness(0);

your main program could deal with just an array of MYFASTLED

Say your class takes 4 parameters at creation time, you just instantiate them and add them into an array.
Then you use a for loop to call actions on the array, or each strip independently if you want

#include "MYFASTLED"

MYFASTLED strip0(param0_0, param0_1, param0_2, param0_3); // parameters that are need for each Strip
MYFASTLED strip1(param1_0, param1_1, param1_2, param1_3);
MYFASTLED strip2(param2_0, param2_1, param2_2, param2_3);
MYFASTLED strip3(param3_0, param3_1, param3_2, param3_3);

MYFASTLED* myStrips[] = {&strip0, &strip1, &strip2,&strip3};

void setup()
{
  for (const auto& aStrip : myStrips) aStrip.begin(); // for example, would ask each strip to set itself up
  strip3.setDirection(REVERSE); // but you can ask individual strips for special things
}

void loop()
{
  for (const auto& aStrip : myStrips) aStrip.animate(); // for example, would ask each strip to animate
}