error LNK2001: unresolved external symbol “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();
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(); }
}
我在哪里使用声音设备
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;
注意:您可能也希望初始化它,例如:
irrklang::ISoundEngine* Sound::_soundDevice = 0;
静态,但非const数据成员应定义在类定义之外,并且在包围类的命名空间内部.通常的做法是在翻译单元(* .cpp)中定义它,因为它被认为是一个实现细节.只有静态和常量整数类型可以同时声明和定义(在类定义中):
class Example { public: static const long x = 101; };
在这种情况下,您不需要添加x定义,因为它已经在类定义中定义.但是,在你的情况下,这是必要的.摘自C标准第9.4.2节:
The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition.