1 registered members (AndrewAMD),
1,306
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
structs A <-> B: forward declaration?
#463290
11/26/16 19:50
11/26/16 19:50
|
Joined: Sep 2003
Posts: 6,861 Kiel (Germany)
Superku
OP
Senior Expert
|
OP
Senior Expert
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
|
Hello! I have two structs, like this:
[???]
struct A
{
int x;
struct B *b;
};
struct B
{
int y;
[...]
struct A *a;
};
struct A now needs a forward declaration of struct B (right?). No matter what I write, the lite-C compiler only gives me error messages (for example a simple "struct B;" results in "B (Struct) undeclared identifier" in the same line). The only workaround I can think of right now is using a void pointer in A. Any ideas/ what am I doing wrong? EDIT: Does it do any harm if I declare/ define struct B first with let's say one dummy member, then define it differently some lines later? Does not work. Second definition is ignored.
Last edited by Superku; 11/26/16 21:58.
"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual Check out my new game: Pogostuck: Rage With Your Friends
|
|
|
Re: structs A <-> B: forward declaration?
[Re: Superku]
#463300
11/27/16 11:58
11/27/16 11:58
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
You could try a forward declaration, not sure if lite-c supports it, though. Otherwise, using a void* might be the only solution.
Edit: looks like lite-c doesn't support it..
Last edited by Kartoffel; 11/27/16 12:03.
POTATO-MAN saves the day! - Random
|
|
|
Re: structs A <-> B: forward declaration?
[Re: Superku]
#463341
11/28/16 21:21
11/28/16 21:21
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
Kinda sucks...
I'm not sure how much code you've already written with your structs (might be a lot of work to change everything) but maybe there's a way to avoid this kind of "loop" definition. I guess it depends on the specific situation but I once ran into the same problem and was able to work my way around it with some changes to the structs I use and how they work together.
POTATO-MAN saves the day! - Random
|
|
|
Re: structs A <-> B: forward declaration?
[Re: Superku]
#463357
11/29/16 18:27
11/29/16 18:27
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
Yeah, I don't see a fancy way of avoiding that problem in this case... so void* is pretty much all that's left. I think I'd use a void pointer (like void * pParentRoom;) in the OBJECT struct since it's probably not being used as much as the pointer for the other direction. And everytime I want to access it I'd just quickly cast it: ROOM * pRoom = object->pParentRoom; pRoom->...But that's probably how you're doing it already so...
Last edited by Kartoffel; 11/29/16 18:35.
POTATO-MAN saves the day! - Random
|
|
|
Re: structs A <-> B: forward declaration?
[Re: Kartoffel]
#463372
11/30/16 12:26
11/30/16 12:26
|
Joined: Sep 2003
Posts: 6,861 Kiel (Germany)
Superku
OP
Senior Expert
|
OP
Senior Expert
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
|
Yeah I'm doing it like that. It's not a game breaker but it's annoying extra work/ lines of code, esp. because there's a bunch of nested structs, now with a void pointer each, already (like door->room->area->superarea and so on).
"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual Check out my new game: Pogostuck: Rage With Your Friends
|
|
|
|