繁体   English   中英

静态初始化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) );

否则你只是声明其他变量,并且函数的静态未定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM