Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error LNK2001: unresolved external symbol "private: static class

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

like image 221
Robbie Avatar asked Apr 17 '13 00:04

Robbie


People also ask

How do I fix unresolved external symbol lnk2001?

To fix this issue, add the /NOENTRY option to the link command. This error can occur if you use incorrect /SUBSYSTEM or /ENTRY settings in your project. For example, if you write a console application and specify /SUBSYSTEM:WINDOWS, an unresolved external error is generated for WinMain .

How do you initialize a static member in C++?

We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.

What is an unresolved external symbol in C++?

The unresolved external symbol is a linker error that indicates it cannot find the symbol or its reference during the linking process. The error is similar to “undefined reference” and is issued interchangeably.


2 Answers

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.

like image 100
Alexander Shukaev Avatar answered Sep 18 '22 00:09

Alexander Shukaev


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.

like image 33
David A. Gray Avatar answered Sep 20 '22 00:09

David A. Gray