Click here to Skip to main content
15,897,891 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Nested structs problem Pin
Richard MacCutchan13-Feb-11 11:41
mveRichard MacCutchan13-Feb-11 11:41 
GeneralRe: Nested structs problem [modified] Pin
Niklas L13-Feb-11 11:52
Niklas L13-Feb-11 11:52 
GeneralRe: Nested structs problem Pin
manchukuo13-Feb-11 12:39
manchukuo13-Feb-11 12:39 
GeneralRe: Nested structs problem Pin
Richard MacCutchan13-Feb-11 13:33
mveRichard MacCutchan13-Feb-11 13:33 
GeneralRe: Nested structs problem Pin
manchukuo13-Feb-11 15:13
manchukuo13-Feb-11 15:13 
GeneralRe: Nested structs problem Pin
Niklas L13-Feb-11 21:04
Niklas L13-Feb-11 21:04 
GeneralRe: Nested structs problem Pin
manchukuo14-Feb-11 12:44
manchukuo14-Feb-11 12:44 
GeneralRe: Nested structs problem Pin
Stefan_Lang14-Feb-11 0:19
Stefan_Lang14-Feb-11 0:19 
Basically it all comes down to the compiler being incapable of reading ahead: the moment you do something you have to make sure that all neccessary defintions have been made before:
struct A;               // 'A' is a symbol used to declare a struct type some time later
struct B;               // same for 'B'
struct A {              // here starts the declaration of A (this makes the forward declaration of A redundant!)
   int gummy;
   B* myb;              // this is a pointer - since all pointers are of same size, no need to more about B here
   A() : myb(0) {}      // initialize a pointer to an unknown type to 0 is not a problem either
   void set(B* b) {
      myb = b;          // again no problem - a pointer is a pointer is a pointer
      gummy = b->bears; // error: compiler does not know about bears at this point!
   }
};
struct B {
   int bears;           // now, had the compiler only known about this before ...
   A* mya;              // ok, just a pointer here
   B() : mya(0) {}      // same boring stuff
   void set(A* a) {
      mya = a;          // pointer stuff, no worries
      bears = a->gummy; // error: compiler hasn't seen a *valid* declaration of struct A yet, and thus doesn't know about gummy
   }
};

You can skip the forward declaration of A here, but you have to move the implemetation of A::set() to a later point in your code, so the compiler gets a chance to read the declaration of B first:
struct B;               // forward declaration for 'B' (none needed for A)
struct A {              // here starts the declaration of A (this makes the forward declaration of A redundant!)
   int gummy;
   B* myb;              // this is a pointer - since all pointers are of same size, no need to more about B here
   A() : myb(0) {}      // initialize a pointer to an unknown type to 0 is not a problem either
   void set(B* b);      // ok: just a prototype declaration, using a pointer to an as of yet undefined struct
};          // end of declaration of struct A: from now on, the compiler knows the full scope of A!
struct B {
   int bears;
   A* mya;              // ok, just a pointer here
   B() : mya(0) {}      // same boring stuff
   void set(A* a) {
      mya = a;          // pointer stuff, no worries
      bears = a->gummy; // ok: compiler has seen the declaration of struct A, and thus knows about gummy
   }
};          // end of declaration of struct B: from now on, the compiler knows the full scope of B!
void A::set( B* b )
{
   myb = b;          // pointers again
   gummy = b->bears; // ok: compiler now knows about bears at this point!
}

Does this help?
GeneralRe: Nested structs problem Pin
manchukuo14-Feb-11 12:48
manchukuo14-Feb-11 12:48 
GeneralRe: Nested structs problem Pin
Richard MacCutchan13-Feb-11 13:28
mveRichard MacCutchan13-Feb-11 13:28 
GeneralRe: Nested structs problem Pin
Niklas L13-Feb-11 19:44
Niklas L13-Feb-11 19:44 
Questionhow to init member class array if the class has no default constrctor? Pin
includeh1013-Feb-11 5:16
includeh1013-Feb-11 5:16 
AnswerRe: how to init member class array if the class has no default constrctor? Pin
Iain Clarke, Warrior Programmer13-Feb-11 6:34
Iain Clarke, Warrior Programmer13-Feb-11 6:34 
AnswerRe: how to init member class array if the class has no default constrctor? Pin
jschell13-Feb-11 8:00
jschell13-Feb-11 8:00 
AnswerRe: how to init member class array if the class has no default constrctor? Pin
CPallini13-Feb-11 21:40
mveCPallini13-Feb-11 21:40 
AnswerRe: how to init member class array if the class has no default constrctor? Pin
Stefan_Lang13-Feb-11 23:19
Stefan_Lang13-Feb-11 23:19 
QuestionMFC: Use firefox in MFC Pin
msn9212-Feb-11 22:39
msn9212-Feb-11 22:39 
AnswerRe: MFC: Use firefox in MFC Pin
wangningyu12-Feb-11 22:47
wangningyu12-Feb-11 22:47 
AnswerRe: MFC: Use firefox in MFC Pin
Andrew Brock12-Feb-11 23:41
Andrew Brock12-Feb-11 23:41 
AnswerRe: MFC: Use firefox in MFC Pin
Richard MacCutchan12-Feb-11 23:42
mveRichard MacCutchan12-Feb-11 23:42 
AnswerRe: MFC: Use firefox in MFC Pin
Maximilien13-Feb-11 1:02
Maximilien13-Feb-11 1:02 
QuestionHow to free the variable Pin
goldenrose912-Feb-11 4:47
goldenrose912-Feb-11 4:47 
AnswerRe: How to free the variable Pin
Yusuf12-Feb-11 5:04
Yusuf12-Feb-11 5:04 
GeneralRe: How to free the variable Pin
goldenrose912-Feb-11 5:10
goldenrose912-Feb-11 5:10 
GeneralRe: How to free the variable Pin
Aescleal12-Feb-11 5:19
Aescleal12-Feb-11 5:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.