c++ - 静态初始化 C++

标签 c++ static initialization

我对 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++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11786217/

相关文章:

c++ - 为什么这个双端队列数据结构会引发读取访问冲突

java - 静态对象中非静态变量的作用域

c++ - 内存管理接口(interface)

Java 静态 ArrayList<Listener>

c++ - 如何在类方法中创建即时相关的静态变量?

c# - 是否应该将此列表初始值设定项行为报告为 Visual Studio C# 编译器中的错误?

c - 之前使用memset还需要设置ptr为NULL吗?

arrays - 在 Julia 中初始化一个包含大量数组的空数组

c++ - 这个指针函数的复杂度是多少?

c++ - 如何在另一个对象的构造函数中声明和创建对象?