class inside class with same class inside :)

Started by
3 comments, last by alex211169 22 years, 4 months ago
hi! problem: class myclass2; // fwd-declaration class myclass1 // definition class 1 { myclass2 *pmc2; // OK because declared myclass2 mc2; // !!! Error: not defined } class myclass2 // definition class 2 { myclass1 *pmc2; // OK already declared myclass2 mc2; // OK already defined } any ideas ?
Advertisement
Just declare myclass2 before myclass1 and change the forward declaration to class myclass1;

class myclass1; // fwd-declarationclass myclass2 // definition class 2{myclass1 *pmc2; // OK already (fwd)declaredmyclass2 mc2; // OK already defined }class myclass1 // definition class 1{myclass2 *pmc2; // OK because declaredmyclass2 mc2; // NOW it should be OK}  


HTH

Edited by - Chappa on December 18, 2001 1:57:23 PM
that's the same problem vice versa

i guess it's a slip of the key that you declared myclass2 inside myclass2 (sorry, my fault)

the corrected version

class myclass2; // fwd-declaration

class myclass1 // definition class 1
{
myclass2 *pmc2; // OK because declared
myclass2 mc2; // !!! Error: not defined
}

class myclass2 // definition class 2
{
myclass1 *pmc1; // OK already declared
myclass1 mc1; // OK already defined
// ^^^^ not myclass2
}


humm?



Edited by - alex211169 on December 19, 2001 12:47:33 PM

Edited by - alex211169 on December 19, 2001 12:54:45 PM
you will end up with an infinite recursive class definition. c''mon, now.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
You should not need to delclare two classes in the way that you are trying to declare them. In fact, you should not declare two classes like you are attempting, because, as jenova said, you will get an infinite recursive class definition. i.e. when you get to the line where you create a class of either type, it will keep having to create new classes until you run out of memory and it throws an exception, or you forcibly stop the execution of the program.
If you change everything to pointers, then you should not have this problem, and your class declarations will work.

jw

This topic is closed to new replies.

Advertisement