Forward decleration problem

Hello all,

I have a header that has pointers to SDL_MouseMotionEvent and
SDL_MouseButtonEvent,
so at the top of the header I’m trying to forward declare these (then in the
.cpp I include the
appropriate header).

I have tried both

struct SDL_MouseMotionEvent; and
typedef struct MouseMotionEvent;

but I keep getting these when using the GCC compiler:

In file included from Lib/Control/InputManager.cpp:11:
Lib/Control/InputManager.h:18: error: conflicting declaration ‘struct
SDL_MouseMotionEvent’
/usr/include/SDL/SDL_events.h:131: error: ‘SDL_MouseMotionEvent’ has a
previous declaration as typedef struct SDL_MouseMotionEvent SDL_MouseMotionEvent' Lib/Control/InputManager.h:19: error: conflicting declaration 'struct SDL_MouseButtonEvent' /usr/include/SDL/SDL_events.h:140: error: 'SDL_MouseButtonEvent' has a previous declaration astypedef struct SDL_MouseButtonEvent
SDL_MouseButtonEvent’

Any ideas?

cheers,
K.

Hello,

I have a header that has pointers to SDL_MouseMotionEvent and
SDL_MouseButtonEvent,
so at the top of the header I’m trying to forward declare these (then in the
.cpp I include the
appropriate header).

Um, this may be a stupid question, but what about including the SDL
header(s) at the beginning of your headers? Is it really necessary to do
forward declaration? Something like that is usually only used to work
around circular declarations, and I doubt SDL code relies on things your
code needs to declare first (though I could be wrong !-)

I have tried both

struct SDL_MouseMotionEvent; and
typedef struct MouseMotionEvent;

Both, as in “at the same time”? What happens if you only declare
struct SDL_MouseMotionEvent;
and comment out the other relevant portions of your code?

In my opinion, the code should be
struct SDL_MouseMotionEvent;
typedef struct SDL_MouseMotionEvent SDL_MouseMotionEvent;
EXCEPT that there may be conflicts trying to pre-declare C structures in
C++ without surronding
extern “C” {
}
lines. But then again, I’m no expert in C++ name mangling…

Still, hope this helps,

Ben Deutsch

Nope…one at a time…to clarify, when I said:

I have tried both

struct SDL_MouseMotionEvent; and
typedef struct MouseMotionEvent;

I ment I have tried both, one at a time (not both at the same time) :wink:

To answer your other question, I don’t like including headers in header
files unless I have to. It creates hude dependancy trees that on
some platforms (e.g. GameCube) are very problematic (especialy
on large projects). So, as a habit, I prefer to use pointers and
references in headers for things I can forward declare and then include
the relevant headers in the implementation files when I actualy need to
use these types.

cheers,
k.> ----- Original Message -----

From: sdl-bounces+kos=climaxgroup.com@libsdl.org
[mailto:sdl-bounces+kos=climaxgroup.com at libsdl.org]On Behalf Of Benjamin
Deutsch
Sent: 07 January 2005 19:12
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] forward decleration problem

Hello,

I have a header that has pointers to SDL_MouseMotionEvent and
SDL_MouseButtonEvent,
so at the top of the header I’m trying to forward declare these (then in
the
.cpp I include the
appropriate header).

Um, this may be a stupid question, but what about including the SDL
header(s) at the beginning of your headers? Is it really necessary to do
forward declaration? Something like that is usually only used to work
around circular declarations, and I doubt SDL code relies on things your
code needs to declare first (though I could be wrong !-)

I have tried both

struct SDL_MouseMotionEvent; and
typedef struct MouseMotionEvent;

Both, as in “at the same time”? What happens if you only declare
struct SDL_MouseMotionEvent;
and comment out the other relevant portions of your code?

In my opinion, the code should be
struct SDL_MouseMotionEvent;
typedef struct SDL_MouseMotionEvent SDL_MouseMotionEvent;
EXCEPT that there may be conflicts trying to pre-declare C structures in
C++ without surronding
extern “C” {
}
lines. But then again, I’m no expert in C++ name mangling…

Still, hope this helps,

Ben Deutsch


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

With MSVC 6 and 7 you can put

#pragma once

at the beginning of the header, and it will only be included once,
regardless of how many other files (or levels) include it.

If you’re not using MSVC, I believe you can wrap your headers like this:

#ifndef MY_HEADER_H
#define MY_HEADER_H

… all your declarations etc here…

#endif /* MY_HEADER_H */

That way again it will only get included once. That should be portable
to any ANSI C++ compiler, and I believe any ANSI C compiler as well.

Hope this helps.

-JustinOn Fri, 7 Jan 2005 19:22:45 -0000, Kostas Kostiadis wrote:

To answer your other question, I don’t like including headers in header
files unless I have to. It creates hude dependancy trees that on
some platforms (e.g. GameCube) are very problematic (especialy
on large projects). So, as a habit, I prefer to use pointers and
references in headers for things I can forward declare and then include
the relevant headers in the implementation files when I actualy need to
use these types.

Hi

Sory if I am misunderstending you, what do you want
meaning ‘forwarding’ is declare at a file and reuse
it?

If is that, you can use extern.-------------
some code:

main.c:

int t = 0;

int main(char argc, char * argv[]) {
/* change value of t, for testing */
t += 10;

/* print the t value (printit is in another file,

  • but has access to t (becuse of extern)
    */
    printit();

return 0;
}

test.c:
extern int t;

void printit() {
printf("%d\n", t);
}

(you should see ‘10’ on the output)

— Kostas Kostiadis escreveu:

Hello all,

I have a header that has pointers to
SDL_MouseMotionEvent and
SDL_MouseButtonEvent,
so at the top of the header I’m trying to forward
declare these (then in the
.cpp I include the
appropriate header).

I have tried both

struct SDL_MouseMotionEvent; and
typedef struct MouseMotionEvent;

but I keep getting these when using the GCC
compiler:

In file included from
Lib/Control/InputManager.cpp:11:
Lib/Control/InputManager.h:18: error: conflicting
declaration ‘struct
SDL_MouseMotionEvent’
/usr/include/SDL/SDL_events.h:131: error:
‘SDL_MouseMotionEvent’ has a
previous declaration as typedef struct SDL_MouseMotionEvent SDL_MouseMotionEvent' Lib/Control/InputManager.h:19: error: conflicting declaration 'struct SDL_MouseButtonEvent' /usr/include/SDL/SDL_events.h:140: error: 'SDL_MouseButtonEvent' has a previous declaration astypedef struct
SDL_MouseButtonEvent
SDL_MouseButtonEvent’

Any ideas?

cheers,
K.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Converse com seus amigos em tempo real com o Yahoo! Messenger

I think you’re missing the point…
The

#ifndef foo
#define foo
#endif

goes without saying…otherwise you get redefintion problems…
What I’m talking about is that with the GNU compiler you can’t do this:

In foo.h

struct SDL_MouseMotionEvent;

class foo
{
void SomeMethod(SDL_MouseMotionEvent* event);
};

which you should be able to…
Then in foo.cpp you can have

#include <SDL/SDL_event.h>
#include <SDL/SDL_mouse.h>

void foo::SomeMethod(SDL_MouseMotionEvent* event)
{
// do something with event here
}> ----- Original Message -----

From: sdl-bounces+kos=climaxgroup.com@libsdl.org
[mailto:sdl-bounces+kos=climaxgroup.com at libsdl.org]On Behalf Of Justin
Coleman
Sent: 07 January 2005 20:10
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] forward decleration problem

On Fri, 7 Jan 2005 19:22:45 -0000, Kostas Kostiadis <@Kostas_Kostiadis> wrote:

To answer your other question, I don’t like including headers in header
files unless I have to. It creates hude dependancy trees that on
some platforms (e.g. GameCube) are very problematic (especialy
on large projects). So, as a habit, I prefer to use pointers and
references in headers for things I can forward declare and then include
the relevant headers in the implementation files when I actualy need to
use these types.

With MSVC 6 and 7 you can put

#pragma once

at the beginning of the header, and it will only be included once,
regardless of how many other files (or levels) include it.

If you’re not using MSVC, I believe you can wrap your headers like this:

#ifndef MY_HEADER_H
#define MY_HEADER_H

… all your declarations etc here…

#endif /* MY_HEADER_H */

That way again it will only get included once. That should be portable
to any ANSI C++ compiler, and I believe any ANSI C compiler as well.

Hope this helps.

-Justin


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl