私は2つのクラスを持っています。まず、(Spawn.cppが唯一のクラスが呼び出されたメッセージを出力):エラーLNK2001別のクラスへの静的ポインタを使用する場合
// Spawn.h
#pragma once
#ifndef SPAWN_H_
#define SPAWN_H_
class Spawn {// const + destr only};
#endif /* SPAWN_H_ */
第二:
// 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_ */
コード自体:
// 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;
}
私はにSDL外部ライブラリを使用しています2つのスレッドを作成すると、これらのスレッドはstatic int
関数なので、静的ポインタ(標準のポインタを使用するとエラーはポインタが静的でなければならない)を使用することは可能ですが、エラーが発生しますグラムのVisual Studio 2015:私はここで行わ提案を試してみました
Error LNK2001 unresolved external symbol "private: static class Spawn * Thread::pSpawner"
Error LNK1120 1 unresolved externals
、ない結果は(あなたはThread.hで宣言と定義を見つけることができます): error LNK2001: unresolved external symbol public: static class
C++ vector issue - 'LNK2001: unresolved external symbol private: static...'
この質問の回答は役に立たなかった(私はすべての提案):
What is an undefined reference/unresolved external symbol error and how do I fix it?
してください[編集] [MCVE]を提供するために、あなたの質問。これは、このようなビルド時間のエラーを多くコード化する方法です。 –
コードのシンボル 'pt'を検索します。リンカーが見つかりません。一般的なイディオムは、変数定義をソースファイルに配置することです。 –
バウムmit Augen、完了。 –