Arthur
Arthur

Reputation: 598

Static initialisation C++

My limited understanding of C++ means that I have no idea how to correctly do the following:

#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;
    }
}

Where ms_pSmall etc are static pointers and Global is a singleton with the following function declarations:

static Global* sharedGlobal();
SDL_Surface* loadImage(const std::string& filename) const;

The code seems to compile correctly but the linker complains with:

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)

[Sorry about the poor indentation.]

Any help here would be appreciated and I will happily show more code, but i think this all that someone needs to understand what I am trying to do.

Thanks in advance!

Upvotes: 3

Views: 152

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258608

You need to qualify the members:

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) );

Otherwise you're just declaring other variables, and the statics from the function are left undefined.

Upvotes: 8

Related Questions