[SOLVED] undefined reference to

Hi I am getting the following error when declaring a class with static members.

Error:

/tmp/ccAgnwnD.ltrans0.ltrans.o: In function `sd_set_distance':

/tmp/445697414/build/sketch/Bicycle_Computer.cpp:116: undefined reference to `BicycleComputer::my_spd_dst'

/tmp/445697414/build/sketch/Bicycle_Computer.cpp:116: undefined reference to `BicycleComputer::my_spd_dst'

collect2: error: ld returned 1 exit status

exit status 1

The code is really long and wont fit in a snippet so I've attached a zip file with all the code, sorry for the inconvenience but there's no other way from my perspective.

Bicycle_computer_nextion.zip (20.8 KB)

ningaman151:
The code is really long and wont fit in a snippet so I've attached a zip file with all the code, sorry for the inconvenience but there's no other way from my perspective.

Make and post an MCVE

Static class member usually requires 2 steps. First you define them inside the class. Second, you declare them outside the class. Example:

class A 
{
   static int num;
};

A::num = 0;

arduino_new:
Static class member usually requires 2 steps. First you define them inside the class. Second, you declare them outside the class.

Code is correct, terminology is wrong. Declare inside, define outside.

arduino_new:
Static class member usually requires 2 steps. First you define them inside the class. Second, you declare them outside the class. Example:

class A 

{
  static int num;
};

A::num = 0;

gfvalvo:
Code is correct, terminology is wrong. Declare inside, define outside.

I am getting this error when defining my variables.

/tmp/365392539/build/sketch/Bicycle_Computer.cpp:18:42: error: expected initializer before '.' token

static BicycleComputer::SpdDst my_spd_dst.spd = 0;

^

/tmp/365392539/build/sketch/Bicycle_Computer.cpp:19:42: error: expected initializer before '.' token

static BicycleComputer::SpdDst my_spd_dst.dst = 0;

^

/tmp/365392539/build/sketch/Bicycle_Computer.cpp:20:45: error: expected initializer before '.' token

static BicycleComputer::SpdDst other_spd_dst.spd = 0;

^

/tmp/365392539/build/sketch/Bicycle_Computer.cpp:21:45: error: expected initializer before '.' token

static BicycleComputer::SpdDst other_spd_dst.dst = 0;

^

exit status 1

The variables 'my_spd_dst' and 'other_spd_dst' are declared in another file.

Here is the relevant code:

static BicycleComputer::SpdDst my_spd_dst.spd = 0;
static BicycleComputer::SpdDst my_spd_dst.dst = 0;
static BicycleComputer::SpdDst other_spd_dst.spd = 0;
static BicycleComputer::SpdDst other_spd_dst.dst = 0;

Here is the relevant code:

class BicycleComputer
{
   struct SpdDst 
    {
      double spd;
      double dst;
    };

    static SpdDst my_spd_dst, other_spd_dst;
};

This is tested code. (Sorry for the inccorect information above)

A.h

#ifndef A_
#define A_

struct SpdDst
{
   double spd;
   double dst;
};

class A
{
   static SpdDst my_spd_dst;
};

#endif

A.cpp

SpdDst A::my_spd_dst {1.5, 2.5};

arduino_new:
This is tested code. (Sorry for the inccorect information above)

A.h

#ifndef A_

#define A_

struct SpdDst
{
  double spd;
  double dst;
};

class A
{
  static SpdDst my_spd_dst;
};

#endif



A.cpp


SpdDst A::my_spd_dst {1.5, 2.5};

Thank you, that solved my issue. I will now mark this as solved :).