error LNK2001:未解析的外部符号“private:static class [英] error LNK2001: unresolved external symbol "private: static class
问题描述
错误LNK2001:未解析的外部符号private:static class irrklang :: ISoundEngine * GameEngine :: Sound :: _ soundDevice(?_soundDevice @ Sound @ GameEngine @@ 0PAVISoundEngine @ irrklang @@ A)
error LNK2001: unresolved external symbol "private: static class irrklang::ISoundEngine * GameEngine::Sound::_soundDevice" (?_soundDevice@Sound@GameEngine@@0PAVISoundEngine@irrklang@@A)
我无法弄清楚为什么我收到此错误。我相信我正确初始化。任何人可以借一只手。
I cannot figure out why i am recieving this error. I believe i am initialising correctly. Can anyone lend a hand.
sound.h
class Sound
{
private:
static irrklang::ISoundEngine* _soundDevice;
public:
Sound();
~Sound();
//getter and setter for _soundDevice
irrklang::ISoundEngine* getSoundDevice() { return _soundDevice; }
// void setSoundDevice(irrklang::ISoundEngine* value) { _soundDevice = value; }
static bool initialise();
static void shutdown();
sound.cpp
sound.cpp
namespace GameEngine
{
Sound::Sound() { }
Sound::~Sound() { }
bool Sound::initialise()
{
//initialise the sound engine
_soundDevice = irrklang::createIrrKlangDevice();
if (!_soundDevice)
{
std::cerr << "Error creating sound device" << std::endl;
return false;
}
}
void Sound::shutdown()
{
_soundDevice->drop();
}
}
并且在那里我使用sounddevice
and where i use the sounddevice
GameEngine::Sound* sound = new GameEngine::Sound();
namespace GameEngine
{
bool Game::initialise()
{
///
... non-related code removed
///
//initialise the sound engine
if (!Sound::initialise())
return false;
任何帮助将非常感谢。
推荐答案
将它放入 sound.cpp
:
irrklang::ISoundEngine* Sound::_soundDevice;
注意:您可能还需要初始化它,例如:
NOTE: You might want to initialize it as well, for example:
irrklang::ISoundEngine* Sound::_soundDevice = 0;
static
,但非 - const
数据成员应该在类定义之外和包含类的命名空间内定义。通常的做法是在翻译单元( *。cpp
)中定义它,因为它被认为是一个实现细节。只有 static
和 const
整数类型可以同时声明和定义(在类定义中):
static
, but non-const
data members should be defined outside of the class definition and inside the namespace enclosing the class. The usual practice is to define it in the translation unit (*.cpp
) because it is considered to be an implementation detail. Only static
and const
integral types can be declared and defined at the same time (inside class definition):
class Example {
public:
static const long x = 101;
};
在这种情况下,您不需要添加 x
定义,因为它已经在类定义的内部定义。但是,在你的情况下,这是必要的。从 C ++标准第9.4.2节中提取:
in this case you don't need to add x
definition because it is already defined inside the class definition. However, in your case it is necessary. Extract from section 9.4.2 of the C++ Standard:
静态数据成员的定义应出现在一个包含成员类定义的命名空间范围。
The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition.
这篇关于error LNK2001:未解析的外部符号“private:static class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!