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

## 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 on other sites
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 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 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 on other sites
Hmm I thought I tried something like that..

Well just so long as youre sure its possible, ill go try some more hehe

##### 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!

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002286
×