错误LNK2001:未解决的外部符号“私有:静态类irrklang::ISoundEngine * GameEngine::Sound::_soundDevice”
我不明白为什么我会收到这个错误。我相信我的初始化是正确的。有人能帮忙吗?
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;任何帮助都将不胜感激。
发布于 2013-04-17 00:17:02
把这个放进sound.cpp
irrklang::ISoundEngine* Sound::_soundDevice;注意:您可能也希望初始化它的,例如:
irrklang::ISoundEngine* Sound::_soundDevice = 0;static,但非const数据成员应该在类定义之外和包含类的命名空间中定义。通常的做法是在翻译单元(*.cpp)中定义它,因为它被认为是一个实现细节。只有static和const积分类型可以同时声明和定义(类内定义):
class Example {
public:
static const long x = 101;
};在这种情况下,您不需要添加x定义,因为它已经在类定义中定义了。然而,在你的情况下,这是必要的。摘自 C++标准第9.4.2节
静态数据成员的定义应出现在包含成员类定义的命名空间范围中。
发布于 2015-03-03 02:58:02
最终,@Alexander给出的答案在我自己的代码中解决了一个类似的问题,但并不是没有几次尝试。为了下一个访问者的利益,当他说“把这个放到sound.cpp中”时,要非常清楚的是,这是除了声音中已经存在的内容之外。
发布于 2021-05-14 14:49:54
堆栈数组定义也有同样的问题。所以,让我在这里简单解释一下。
在头文件中:
class MyClass
{
private:
static int sNums[55]; // Stack array declaration
static int* hNums; // Heap array declaration
static int num; // Regular variable declaration
}在C++文件中
int MyClass::sNums[55] = {}; // Stack array definition
int MyClass::hNums[55] = new int[55]; // Heap array definition
int MyClass::num = 5; // Regular variable Initializationhttps://stackoverflow.com/questions/16049306
复制相似问题