Rob*_*bie 14 c++ static-members linker-errors visual-c++
错误LNK2001:未解析的外部符号"private:static class irrklang :: ISoundEngine*GameEngine :: Sound :: _ soundDevice"(?_soundDevice @ Sound @ GameEngine @@ 0PAVISoundEngine @ irrklang @@ A)
我无法弄清楚为什么我收到这个错误.我相信我正在初始化.任何人都可以伸出援手吗?
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();
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
我在哪里使用声音设备
GameEngine::Sound* sound = new GameEngine::Sound();
namespace GameEngine
{
bool Game::initialise()
{
///
/// non-related code removed
///
//initialise the sound engine
if (!Sound::initialise())
return false;
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激
Ale*_*aev 46
把它放入sound.cpp
:
irrklang::ISoundEngine* Sound::_soundDevice;
Run Code Online (Sandbox Code Playgroud)
注意:您可能也想要初始化它,例如:
irrklang::ISoundEngine* Sound::_soundDevice = 0;
Run Code Online (Sandbox Code Playgroud)
static
,但非const
数据成员应该在类定义之外和包含该类的命名空间内定义.通常的做法是在翻译单元(*.cpp
)中定义它,因为它被认为是一个实现细节.只能static
和const
整数类型可以同时声明和定义(在类定义内):
class Example {
public:
static const long x = 101;
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您不需要添加x
定义,因为它已在类定义中定义.但是,在您的情况下,这是必要的.摘自C++标准的9.4.2节:
静态数据成员的定义应出现在包含成员类定义的命名空间范围内.
最终,@Alexander 给出的答案在我自己的代码中解决了一个类似的问题,但并非没有几次试验。为了下一位访问者的利益,当他说“将其放入 sound.cpp”时,要完全清楚,这是对 sound.h 中已存在的内容的补充。
归档时间: |
|
查看次数: |
36206 次 |
最近记录: |