Jump to content
  • Advertisement
Sign in to follow this  
ZealousEngine

#include order madness... [Solved]

This topic is 4006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a struct..
Quote:
#include "SomeClass.h" struct MyStruct { SomeClass a; int b; };
Now, I want SomeClass to be 'aware' of the MyStruct that it belongs to, so it can have access to the OTHER values in MyStruct (like int b). While it IS possible to pass a MyStruct pointer to SomeClass (if I include a forward declaration in the SomeClass header), I cant actually USE the pointer inside SomeClass (I guess since, at the time SomeClass is compiled, MyStruct hasnt officially been declared yet). Is there anyway to get this working? Did anyone follow what im trying to do? heh.. *Btw I realize a 'better' solution might be to just pass a pointer to 'int b' in to SomeClass a. But that could be a lot of extra work if there are several members in MyStruct. It would be so much more convenient to just pass a pointer to the whoel struct. [Edited by - ZealousEngine on October 28, 2007 12:17:01 AM]

Share this post


Link to post
Share on other sites
Advertisement
Just use forward declaration and it should work:


SomeClass.h:

struct MyStruct;

struct SomeClass
{
MyStruct* myStruct;
};

SomeClass.cpp:

#include "SomeClass.h"
#include "MyStruct.h"

... safe to use MyStruct here.

MyStruct.h:

#include "SomeClass.h"

struct MyStruct {
SomeClass a;
int b;
};

Share this post


Link to post
Share on other sites
Yes the above works, you CAN create a POINTER to MyStruct inside SomeClass. However the problem seems to be when you try and USE the MyStruct pointer inside SomeClass. Since youve only done a empty forward declaration, SomeClass has no idea what the exact structure of MyClass looks like.

Right?

*And again, just to sum up - The ultimate goal is to have a Object to have a pointer to the Struct it belongs to.

Share this post


Link to post
Share on other sites
The Thing is that you have to not only declare the struct before you use it but define it. so, you need to define the struct in a header, that is included before it is used. the main problem is that SomeClass.h probally has code that accesses the member variables. the easiest way around this is to put that code in a cpp, which includes you're MyStruct header. Hope that makes sense.

also, deathkrush, just finished stuntman ignition, enjoyed it!

Share this post


Link to post
Share on other sites
Aha! You are correct sir!

So the lesson I learned (and should have already known) is that a forward declaration lets you delare POINTERS to said class, however, any reference to those pointers needs to be put in a cpp. So my problem was I was inlining some functions in the header of my 'SomeClass', which had a forward declaration of the MyStruct, but couldnt actually USE the MyStruct pointer.

Tossing all that code into a SomeClass.cpp solved the problem, because the MyStruct definition is attached as a header at the top!

Thanks again for the help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!