tags:

views:

51

answers:

1

I know I could just make all the Mix_Musics public, and not worry about the problem, but I'd still like to understand how to do it.

   //header.h

    class Music
    {
        private:

         Mix_Music * BGMusic, * fall, * reset, * teleport, * win, * singleCubeWin;

        public:

         Music();

         bool loadMusic();
         void clean_up();

         Mix_Music * getSound( Mix_Music * m ) { return m; }
    };


   //program.cpp

    Music Sound;

    int main( int argc, char* args[] )
    {
        ...

        Mix_PlayMusic( Sound.getSound( "BGMusic" ), -1 );

        ...
    }
+1  A: 

From your code above I'm not absolutely certain what you are trying to do. The function 'getSound' takes a Mix_Music object as the parameter and returns the same object. Now from some deduction I assume that you are trying to request the BGMusic object via a string. There a few ways to do this, via IDs for each of Mix_Music objects, request by ID.:

... // Somewhere above:

enum MixMusicID {
    BGMUSIC,
    FALL,
    RESET,
    TELEPORT,
    WIN,
    SINGLECUBEWIN
};

... // In the class:

Mix_Music * getMusic ( MixMusicID id )
{
    switch (id)
    {
    case BGMUSIC:
     return BGMusic;
     ...
    default:
     return NULL;
    }
}

... // In main:
Mix_PlayMusic( Sound.getSound( BGMUSIC ), -1 );

You could do it similarly with string identifiers for each object. What it really comes down to is there is no built in relationship between a variable's name and a string identifier. So its up to you to implement this relationship either via an enum (above) or string identifiers.

Hope this helped, again not sure exactly what the question was.

DeusAduro
Yeah, that's what I was trying to do is request a sound by its name (string), and have the function return that sound. I'll try out that enum, but I've never used enum. Where do I put that? In main()?
Justen
alright, I added enum to the public declarations of class Music, and when I try Mix_PlayMusic( Sound.getSound( BGMUSIC ), -1 ); from main, it says that BGMUSIC is an undeclared identifier
Justen
Nevermind, I just put it above the Music class at the top of the header file ( the enum ), and it works. Thanks for the help.
Justen
Ya the enum can go anywhere really, so long as it is defined before you actually use it. Above the class definition is good.
DeusAduro