使用指向另一个的静态指针时出现错误 LNK2001 class
error LNK2001 when using a static pointer to another class
我有两个 class。首先(Spawn.cpp 只打印 class 被调用的消息):
// 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 外部库创建两个线程,这些线程是 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?
这个错误信息很清楚,你没有定义你的全局变量。最好在cpp文件中定义一个全局。因为有了header,每次包含header时他都可以被定义。
// Thread.cpp
Spawn * Thread::pSpawner = nullptr;
用#pragma once
和#ifndef
也没用。只做其中一项。
#pragma once
顺便说一下,如果您使用#ifndef ALL 必须在#if 内。你在外面包含了"Spawn.h"
,它会产生无限循环的include。
"There is no advantage to use of both the #include guard idiom and #pragma once in the same file."
我有两个 class。首先(Spawn.cpp 只打印 class 被调用的消息):
// 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 外部库创建两个线程,这些线程是 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?
这个错误信息很清楚,你没有定义你的全局变量。最好在cpp文件中定义一个全局。因为有了header,每次包含header时他都可以被定义。
// Thread.cpp Spawn * Thread::pSpawner = nullptr;
用
#pragma once
和#ifndef
也没用。只做其中一项。#pragma once
顺便说一下,如果您使用#ifndef ALL 必须在#if 内。你在外面包含了
"Spawn.h"
,它会产生无限循环的include。"There is no advantage to use of both the #include guard idiom and #pragma once in the same file."