error LNK2001: unresolved external symbol "private: static class
Asked Answered
B

4

22

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

Barbitone answered 17/4, 2013 at 0:9 Comment(0)
H
65

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.

Haslet answered 17/4, 2013 at 0:17 Comment(2)
Perfect! can now compile and getting the expected result. Thank you very much.Barbitone
criss cest dla marde! :)Estheresthesia
Q
9

Eventually, the answer given by @Alexander resolved a similar issue in my own code, but not without a few trials. For the benefit of the next visitor, when he says "Put this into sound.cpp," to be perfectly clear, this is in addition to what is already present in sound.h.

Quinquagesima answered 3/3, 2015 at 2:58 Comment(0)
M
0

I had the same issue with stack array definition. So, let me explain it briefly here.

In the header file:

class MyClass
{
private:
    static int sNums[55]; // Stack array declaration
    static int* hNums;    // Heap array declaration
    static int num;       // Regular variable declaration
}

In the C++ file

int MyClass::sNums[55] = {};          // Stack array definition
int MyClass::hNums[55] = new int[55]; // Heap array definition
int MyClass::num = 5;                 // Regular variable Initialization
Melbourne answered 14/5, 2021 at 14:49 Comment(0)
Q
0

If you can use C++17, you can declare inline static data member:

private:
inline static irrklang::ISoundEngine* _soundDevice;
Quassia answered 30/3, 2023 at 10:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.