C++ 错误 LNK2001:未解析的外部符号“私有:静态类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16049306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:01:52  来源:igfitidea点击:

error LNK2001: unresolved external symbol "private: static class

c++visual-c++linker-errorsstatic-members

提问by Robbie

error LNK2001: unresolved external symbol "private: static class irrklang::ISoundEngine * GameEngine::Sound::_soundDevice" (?_soundDevice@Sound@GameEngine@@0PAVISoundEngine@irrklang@@A)

错误 LNK2001:未解析的外部符号“私有:静态类 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

声音.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

任何帮助将不胜感激

回答by Alexander Shukaev

Put this into sound.cpp:

把这个放入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-constdata 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 staticand constintegral types can be declared and defined at the same time (inside class definition):

static,但非const数据成员应该在类定义之外和包含类的命名空间内定义。通常的做法是在翻译单元 ( *.cpp) 中定义它,因为它被认为是一个实现细节。只有staticconst整型可以声明,并在同一时间(内部类的定义)定义:

class Example {
public:
  static const long x = 101;
};

in this case you don't need to add xdefinition 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:

在这种情况下,您不需要添加x定义,因为它已经在类定义中定义了。但是,在您的情况下,这是必要的。摘自C++ 标准第 9.4.2 节

The definition for a static data member shall appear in a namespace scope enclosing the member's class definition.

静态数据成员的定义应出现在包含成员类定义的命名空间范围内。

回答by David A. Gray

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.

最终,@Alexander 给出的答案在我自己的代码中解决了类似的问题,但并非没有尝试。为了下一位访问者的利益,当他说“将其放入 sound.cpp”时,要完全清楚,这是对 sound.h 中已存在的内容的补充。