静态初始化C++
Static initialisation C++
我对C++的理解有限,这意味着我不知道如何正确地执行以下操作:
#include "Platform.h"
#include "Global.h"
SDL_Surface *ms_pSmall (Global::sharedGlobal()->loadImage(RESOURCE_SMALL_PLATFORM) );
SDL_Surface *ms_pMedium(Global::sharedGlobal()->loadImage(RESOURCE_MEDIUM_PLATFORM));
SDL_Surface *ms_pLarge (Global::sharedGlobal()->loadImage(RESOURCE_LARGE_PLATFORM) );
//Initialise platform variables
Platform::Platform()
{
int imgSize = rand() % 3;
switch (imgSize)
{
case 2:
m_pImage = ms_pSmall;
break;
case 1:
m_pImage = ms_pMedium;
break;
case 0:
m_pImage = ms_pLarge;
break;
}
}
其中ms_pSmall等是静态指针,Global是具有以下函数声明的单例:
static Global* sharedGlobal();
SDL_Surface* loadImage(const std::string& filename) const;
代码似乎编译正确,但链接器抱怨:
Undefined symbols for architecture i386:"Platform::ms_pMedium", referenced from:
Platform::Platform() in Platform.o "Platform::ms_pLarge", referenced from:
Platform::Platform() in Platform.o "Platform::ms_pSmall", referenced from:
Platform::Platform() in Platform.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[很抱歉压痕不好。]
这里的任何帮助都将不胜感激,我会很高兴地展示更多的代码,但我认为这一切都是有人需要了解我正在尝试做什么。
提前感谢!
您需要对成员进行资格鉴定:
SDL_Surface* Platform::ms_pSmall (Global::sharedGlobal()->loadImage(RESOURCE_SMALL_PLATFORM) );
SDL_Surface* Platform::ms_pMedium(Global::sharedGlobal()->loadImage(RESOURCE_MEDIUM_PLATFORM));
SDL_Surface* Platform::ms_pLarge (Global::sharedGlobal()->loadImage(RESOURCE_LARGE_PLATFORM) );
否则,您只是在声明其他变量,而函数中的静态变量是未定义的。
相关文章:
- 是否可以初始化不可复制类型的成员变量(或基类)
- C++使用整数的压缩数组初始化对象
- C++初始化基类
- 多成员Constexpr结构初始化
- 复制列表初始化的隐式转换的等级是多少
- 内联映射初始化的动态atexit析构函数崩溃
- 如何在C++中初始化嵌套类中的2个memeber
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 没有用于初始化C++中的变量模板的匹配构造函数
- 在未初始化映射的情况下,将值插入到映射的映射中
- C++成员初始化
- 为什么在C++中首先初始化成员类
- 同时具有"聚合初始化"和"模板推导"
- 初始化具有非默认构造函数的std::数组项的更好方法
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 在C和C++中初始化结构中的数组
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 在函数内部的声明中初始化数组,并在外部使用它
- 继承:构造函数,初始化C++11中基类的类C数组成员