0

Hi guys, I am using PlatformIO for programming an arduino. The problem is that I have a static void that should change the static variable but after compiling the code I faced with the error in the vscode saying:

c:/users/hossein/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\huzzah\src\main.cpp.o:(.text.loop+0x0): undefined reference to sampleClass::buffer' collect2.exe: error: ld returned 1 exit status *** [.pio\build\huzzah\firmware.elf] Error 1

I have also included my code in the following box. I would appreciate if anyone can give me a hint such that I can get my script to work.

#include <Arduino.h>

class sampleClass
{
public:
  //variables
  static int buffer[10];
  static float ratio;
  int64_t data;
  // functions
  static void filling()
  {
    sampleClass::buffer[1] = sampleClass::buffer[1] + 1;
    printf(" the value of the buffer in the scope of the function is : %f \n", double(buffer[1]));
  };

private:
};

sampleClass a;

void setup()
{
  // put your setup code here, to run once:
}

void loop()
{
  // put your main code here, to run repeatedly:
  a.sampleClass::filling();
}
Mhkazemi
  • 1
  • 4
  • You need to define `buffer` in a translation unit (cpp file, e.g. the one you're in). `static int buffer[10];` is just a declaration. – pallgeuer Apr 21 '20 at 09:40
  • it worked thanks, but can you say why I should do that ? buffer is a public static variable of the class so why I am getting this error ? and why this way of implementation works ? – Mhkazemi Apr 21 '20 at 09:48
  • It's like declaring a function using `void foo();`. You get a linker error if use it but don't also _define_ it somewhere using `void foo() { ... }`. `static int buffer[10];` tells the compiler/linker that the class should have a member of that type called `buffer`, but then you need to actually provide it using something like `int sampleClass::buffer[] = {0,};` outside the class in a cpp file (e.g. your `main.cpp`). – pallgeuer Apr 21 '20 at 09:53

0 Answers0