3

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!

Arthur
  • 598
  • 1
  • 7
  • 18

1 Answers1

8

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Thanks! such stupidity on my part :( - I just want to tick your answer but i have to wait 10 more minutes. – Arthur Aug 02 '12 at 21:44
  • I do have another issue now which I would appreciate help with, I have shoved a couple of breakpoints in and loadImage() seems to be working correctly but when it comes to setting the value of m_pImage all of the statics are NULL pointers - Any clue why that might be? – Arthur Aug 02 '12 at 21:59
  • 1
    @ArthurFox perhaps http://stackoverflow.com/questions/335369/finding-c-static-initialization-order-problems – Luchian Grigore Aug 02 '12 at 22:07
  • @ArthurFox and if it's not that, you'll have to post a new question. – Luchian Grigore Aug 02 '12 at 22:07
  • If you have a static instance of `Platform`, your code is horribly broken. See Luchian's link two comments up. – David Schwartz Aug 02 '12 at 22:21