user66283: I have spent the better part of today trying to figure out why I can't get this to work...
I have this project where, because of previous design considerations, have implemented a singleton class that initializes a bunch of separate sensors in this system that I am running.
The system was originally built for the Particle Boron, but now I am trying to make it work with the Adafruit Feather M0, so I have had to refactor several things and omit lots of lines of Particle RTOS specific things.
However, in doing this, I find that my singleton class no longer works.
Sensors.h:
.
.
.

class Sensors{

    static Sensors *instance;

public:
    static Sensors *getInstance() {
        if (!instance) {
            instance = new Sensors;
        }
        return instance;
    }

}

private:
    Sensors();

.
.
.

Sensors.cpp:
.
.
.

Sensors::Sensors() : dht22(DHTPIN, DHTTYPE) {
#ifdef PLATFORM_ID
    sensorLog("app.Sensors");
#endif
}

.
.
.

main.ino:
.
.
.

// Sensors
Sensors *Sensors::instance = nullptr;
Sensors *allSensors = allSensors->getInstance();

void setup(){

.
.
.

As I try to compile this with ArduinoISP, no matter what I try, I get the following error:
sketch/main.ino.cpp.o: in function `Sensors::getInstance()':
sketch/lib/Sensors/src/Sensors.h:23: undefined reference to `Sensors::Sensors()'
collect2: error: ld returned 1 exit status
exit status 1
[Error] Exit with code=1

I have tried things like changing the line
instance = new Sensor;

to
instance = new Sensor();

making the instance public, changing the Sensor constructor, but all of this to no avail. Could this be a compiler issue? Why would this work on a Particle board, but not an Adafruit Feather M0?

user77392: I had the same problem when referencing classes from multiple files, remember to put at the top of your header file (ending in .h) #pragma once on files that get referenced more than once!
Edit1: Also after you declare your class you need to specify where your static variable will be stored.
After the class declaration write:
type *Class::variable;
To declare the static variable.
For example:
#pragma once
//#includes

class Screen
{
protected:
  static void (*StateMachineStep)(int);
  static SSOLED *oled;
  static Player *player;

public:
  Screen() {}

};

void (*Screen::StateMachineStep)(int);
SSOLED *Screen::oled;
Player *Screen::player;

Edit2: formatting

66283: <p>I have spent the better part of today trying to figure out why I can't get this to work...</p>
<p>I have this project where, because of previous design considerations, have implemented a singleton class that initializes a bunch of separate sensors in this system that I am running.</p>
<p>The system was originally built for the Particle Boron, but now I am trying to make it work with the Adafruit Feather M0, so I have had to refactor several things and omit lots of lines of Particle RTOS specific things.</p>
<p>However, in doing this, I find that my singleton class no longer works.</p>
<p><strong>Sensors.h:</strong></p>
<pre><code>.
.
.

class Sensors{

    static Sensors *instance;

public:
    static Sensors *getInstance() {
        if (!instance) {
            instance = new Sensors;
        }
        return instance;
    }

}

private:
    Sensors();

.
.
.
</code></pre>
<p><strong>Sensors.cpp:</strong></p>
<pre><code>.
.
.

Sensors::Sensors() : dht22(DHTPIN, DHTTYPE) {
#ifdef PLATFORM_ID
    sensorLog(&quot;app.Sensors&quot;);
#endif
}

.
.
.
</code></pre>
<p><strong>main.ino:</strong></p>
<pre><code>.
.
.

// Sensors
Sensors *Sensors::instance = nullptr;
Sensors *allSensors = allSensors-&gt;getInstance();

void setup(){

.
.
.
</code></pre>
<p>As I try to compile this with ArduinoISP, no matter what I try, I get the following error:</p>
<pre><code>sketch/main.ino.cpp.o: in function `Sensors::getInstance()':
sketch/lib/Sensors/src/Sensors.h:23: undefined reference to `Sensors::Sensors()'
collect2: error: ld returned 1 exit status
exit status 1
[Error] Exit with code=1
</code></pre>
<p>I have tried things like changing the line</p>
<pre><code>instance = new Sensor;
</code></pre>
<p>to</p>
<pre><code>instance = new Sensor();
</code></pre>
<p>making the <code>instance</code> public, changing the Sensor constructor, but all of this to no avail. Could this be a compiler issue? Why would this work on a Particle board, but not an Adafruit Feather M0?</p>
 77392: <p>I had the same problem when referencing classes from multiple files, remember to put at the top of your header file (ending in .h) <code>#pragma once</code> on files that get referenced more than once!</p>
<p>Edit1: Also after you declare your class you need to specify where your static variable will be stored.
After the class declaration write:</p>
<p>type *Class::variable;</p>
<p>To declare the static variable.</p>
<p>For example:</p>
<pre><code>#pragma once
//#includes

class Screen
{
protected:
  static void (*StateMachineStep)(int);
  static SSOLED *oled;
  static Player *player;

public:
  Screen() {}

};

void (*Screen::StateMachineStep)(int);
SSOLED *Screen::oled;
Player *Screen::player;
</code></pre>
<p>Edit2: formatting</p>
