I have a two classes. First (Spawn.cpp only prints the message that class was called):
// Spawn.h
#pragma once
#ifndef SPAWN_H_
#define SPAWN_H_
class Spawn {// const + destr only};
#endif /* SPAWN_H_ */
Second:
// Thread.h
#include "Spawn.h"
#pragma once
#ifndef THREAD_H_
#define THREAD_H_
class Thread
{
public:
Thread();
virtual ~Thread();
private:
void Initialise();
static int WindowThread(void *ptr);
static int ConsoleThread(void *ptr);
static Spawn * CreateSpawner();
static Spawn * pSpawner; // if the pointer is non-static, it can't be
// used within static function.
SDL_Thread * pConsoleThread;
};
Spawn * Thread::pSpawner = nullptr;
#endif /* THREAD_H_ */
The code itself:
// Thread.cpp
#include "stdafx.h"
#include "Thread.h"
Thread::Thread() { Initialise(); }
void Thread::Initialise() { // call of the threads here }
int Thread::WindowThread(void * ptr)
{
while (true)
{
// I want to access pointer pSpawner here
}
return 1;
}
int Thread::ConsoleThread(void * ptr)
{
// Main console loop
while (true)
{
/* Spawn handling */
if (CUI.INP == "spawn")
{
pSpawner = CreateSpawner(); // I am unable to access that
// pointer here as well
}
}
return 2;
}
Spawn * Thread::CreateSpawner()
{
// Singleton initialisation:
Spawn * m_pSPAWNER = new Spawn;
return m_pSPAWNER;
}
I am using the SDL external library to create two threads, these threads are static int
functions, so it's possible to use only static pointer (if I use a standard pointer the error is that the pointer must be static), but the error I get using Visual studio 2015:
Error LNK2001 unresolved external symbol "private: static class Spawn * Thread::pSpawner"
Error LNK1120 1 unresolved externals
I have tried the suggestions done here, but no result (you can find declaration and definition in the Thread.h): error LNK2001: unresolved external symbol public: static class
C++ vector issue - 'LNK2001: unresolved external symbol private: static...'
The answers from this question didn't helped (I will leave an update here when I will try all of the suggestions):
What is an undefined reference/unresolved external symbol error and how do I fix it?