error LNK2001: unresolved external symbol "private: static class

c++, linker-errors, static-members, visual-c++

Solution

Put this into `sound.cpp`:

irrklang::ISoundEngine* Sound::_soundDevice;

NOTE: You might want to initialize it as well, for example:

irrklang::ISoundEngine* Sound::_soundDevice = 0;

`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;
};

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.

Problem

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 receiving this error. I believe i am initializing 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 ``` 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(); } ``` and where i use the sound device ``` GameEngine::Sound* sound = new GameEngine::Sound(); namespace GameEngine { bool Game::initialise() { /// /// non-related code removed /// //initialise the sound engine if (!Sound::initialise()) return false; ``` Any help would be greatly appreciated

Original source