Compile-time error when forward declaring Mix_Chunk

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–
GBGames’ Blog, An Indie Game Developer’s Somewhat Interesting Thoughts:
http://www.gbgames.com/blog
Staff Reviewer for Game Tunnel, the Independent Underground:
http://www.gametunnel.com

I should also specify that I am using SDL v1.2.11 on Debian Gnu/Linux.
I apologize for leaving those details out.–
GBGames’ Blog, An Indie Game Developer’s Somewhat Interesting Thoughts:
http://www.gbgames.com/blog
Staff Reviewer for Game Tunnel, the Independent Underground:
http://www.gametunnel.com

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

Vassilis Virvilis wrote:

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.

Ah, thank you for clarifying this issue. I was going to try to include
the header in my header, but I didn’t want to do so without
understanding why I felt I had to do so. Now I see it is pretty much
required.

Thanks again,
Gianfranco–
GBGames’ Blog, An Indie Game Developer’s Somewhat Interesting Thoughts:
http://www.gbgames.com/blog
Staff Reviewer for Game Tunnel, the Independent Underground:
http://www.gametunnel.com