[SDL] Compile-time error when forward declaring Mix_Chunk
Vassilis Virvilis
vasvir at iit.demokritos.gr
Thu Oct 5 23:45:57 PDT 2006
Gianfranco Berardi wrote:
> I recently started to learn how to use SDL_mixer, and so I followed a
> tutorial to get up to speed. It seems most tutorials assume that you'd
> want to declare the pointer to Mix_Chunk right before the code you use
> it with, but I wanted to add a Mix_Chunk pointer as a member of a class.
>
> Like I usually do when I add a pointer as a member, I forward declared
> the type and included the relevant header in the .cpp file for the
> class. Specifically, I have a Game class, and I want to add a member:
>
> Mix_Chunk * sound_;
>
> So at the top of the header, I wrote:
>
> struct Mix_Chunk; // It could be class Mix_Chunk; The same error occurs
>
> And in the game.cpp file, I include SDL.h and SDL_mixer.h.
>
> When I try to compile, I get the following error:
>
> g++ -c game.cpp -Wall -Wno-unknown-pragmas -Wno-format -g -DDEBUG
> -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT
> /usr/include/SDL/SDL_mixer.h:90: error: conflicting declaration 'typedef
> struct Mix_Chunk Mix_Chunk'
> game.h:36: error: 'Mix_Chunk' has a previous declaration as 'typedef
> struct Mix_Chunk Mix_Chunk'
>
> Any idea why I can't forward declare Mix_Chunk? I imagine it is
> something simple that I've missed, but I'm at a loss so far.
>
> Thank for your time,
> Gianfranco
>
If it is typedefed you can't do that. You have to include the SDL_Mixer.h
header in your class definition header. I am not sure why. Sorry...
The point is with
-my SDL_Mixer 1.2.6 and SDL 1.2.11 in debian unstable
-and SDL_Mixer from svn checkout tree
I can't see this. What I see is in SDL_mixer.h
struct Mix_Chunk {
... /* the actual declaration */
} Mix_Chunk;
Notice that Mix_Chunk is in both tag and name. In C++ struct tags are
first class citizens and do not need a typedef. In C there are two
completely different namespaces. This is why writing code like that
is a good practice if you want to be C++ friendly.
Now on the other hand The Mix_Music is declared like this
typedef struct _Mix_Music Mix_Music;
In my setup I can't forward declare Mix_Music;
The reason people are using this typedef trick is to provide an
opaque pointer. This is a pointer where the programmer cannot reach
the internal variables freely. Unfortunately in C++ which
has proper access mechanisms (private, protected) that means
we have to drag the whole header file in our headers.
.bill
More information about the SDL
mailing list