3

whenever I try to create Mix_Music instance, I get this error: "incomplete type is not allowed".

However, I need to get the address of the pointer music before calling Mix_LoadMUS(file);

Code:

Mix_Music *music;

/* I need the memory address here */

music = Mix_LoadMUS(file);

How do I do this?

Klokop
  • 31
  • 2
  • 1
    1. Include the header file(s) that declares *Mix_Music* and *Mix_LoadUS*. 2. Apparently you don't need the address of the pointer music; because you only show one function and it doesn't need the address of the pointer. 3. The code can be simplified to `Mix_Music *music = Mix_loadMUS(file);` – frayser Nov 21 '10 at 05:52
  • You are right, I don't show the whole code. The point is that I need to pass the memory address to a thread which will load the file there. So I would like to add an empty Mix_Music to some kind of a list/map and then pass memory address to thread. It's up to the thread whether it will load the file there or not. It works fine with other classes but I don't know how to do it with Mix_Music. Headers are included. – Klokop Nov 21 '10 at 19:07
  • 1
    The address of "music", which is "&music", and the address of an empty "Mix_Music" are different things. "&music" is a the address of a *pointer to nowhere* until Mix_LoadMUS() is called. Afterward "&music" points to the address of a newly allocated *struct _Mix_Music*. "Mix_Music mmus;" would allocate an empty *Mix_Music* on the stack. Using *static* or *malloc()* , or *new* would put it elsewhere. Its address would be "&mmus"; But that is the address of an arbitrary *Mix_Music,* not one from MIX_LoadMUS(). It is not a useful address, as Mix_LoadMUS() allocated its own structs. – frayser Nov 22 '10 at 15:40
  • Having *music* as a pointer in an array, means thread needs to access the array and its index. See the updated answer; it's too much to insert here. – frayser Nov 22 '10 at 17:43

1 Answers1

4

Incomplete Type

#include "SDL_mixer.h" and it should be fine1,2.

The compiler is not capable of compiling SDL-related code without the SDL includes to tell it what those SDL refernces (Mix_Musi, Mix_LoadMUS, etc) refer too. See the SDL_Mixer Tutorial at kekkai.org/roger3 It has a complete example.

1 SDL Include file
2 Mix_LOadMUS
3 SDL Tutorial with complete example

--

Update: Using an Array of Music Items

This is an example of how to access a particular pointer to Mix_ Music from within a thread's code, or in any place lexically separate from the allocation of the pointer variable. An actual implementation may want to use dynamic array allocation and needs to add error handling for file-not-found or failed-to-load, etc.

MEnt.h A common iclude file for the initilization and thread modules:

#include <cstdlib>
#include "SDL.h"
#include "SDL_mixer.h"

enum { MAXENTRIES=1024 };
struct MEnt{ 
       Mix_Music * music;
       char *filename;
};

extern MEnt Marray[MAXENTRIES];
extern int Mselected;

Program initialization:

#include "MEnt.h"

// Alocate space for array of music items

MEnt Marray[MAXENTRIES]; 
int Mselected=-1;

In the thread's code,include:

#include "MEnt.h"
// Return a pointer for the selected music item:
// Allocate new Mix_Music* if not already done,
// otherwise return the already allocated pointer.
Mix_Music *getSelected(){
    Mix_Music *music;

    if(Mselected >= 0 && Mselected < MAXENTRIES){
      struct MEnt &current=Marray[Mselected];
       if(!(music=current.music) &&
                  (current.filename!=NULL))
          music=current.music=
                  Mix_LoadMUS(current.filename);
    }
    return music;
}      
frayser
  • 1,754
  • 10
  • 17