|
-
September 25th, 1999, 11:03 PM
#1
Class Inclusion Confusion
Ok, I have 2 classes. ClassA and ClassB. ClassA has it's interface defined in ClassA.h and likewise ClassB is ClassB.h. ClassA has it's implementation in ClassA.cpp and likewise ClassB is in ClassB.cpp.
Here are the classes.
class ClassA
{
public:
ClassA();
ClassB classb;
};
class ClassB
{
public:
ClassB();
ClassA classa;
};
As you probably know, when I told VC++ to add the variables it added these lines in the two .h file.
/ccode]// ClassA.h
#include "ClassB.h" // Added by ClassView
...
// ClassB.h
#include "ClassA.h" // Added by ClassView
...[/ccode]
The, however, will not compile. The compiler says "missing ';' before identifier 'classa'" This is of course because of the circular includes which result in the other class not being defined. So I changed the includes to
// ClassA.h
class ClassB;
// ClassB.h
class ClassA;
This time the compiler said "'classb' uses undefined class 'CClassB'". I supposed the class has to actually be defined not just declared. I decided to try one more thing. I changed the variables in the two classes to pointers.
class ClassA
{
public:
ClassA();
ClassB* classb;
};
class ClassB
{
public:
ClassB();
ClassA* classa;
};
This was able to compiler (however with the includes changed to what I mentioned earlier, the class declarations). Considering how obvious this problem is why doesn't VC++ 6.0 add class ClassName; instead of the include whenever you add a pointer to a class? Also, is there a way to get this to work when variables are an instance of an object rather then a pointer to an object?
Scott
Give a man a fish and feed him for a day,
Teach a man to fish and feed him for life.
_______________________________________________________
http://welcome.to/scottweb - The Ultimate Programmers Reference
-
September 26th, 1999, 05:37 AM
#2
Re: Class Inclusion Confusion
It should compile and work, no matter if the member is an object or pointer to opbject,if u put
#include "classb.h"
in classa.h
and
class ClassA
in classb.h
-
September 26th, 1999, 06:34 AM
#3
Re: Class Inclusion Confusion
That'll get ClassA.cpp to compile but ClassB.cpp still won't. Could you actually take a look at the project. I have it at.http://members.xoom.com/thescott01/public/classtest.zip
Scott
Give a man a fish and feed him for a day,
Teach a man to fish and feed him for life.
_______________________________________________________
<A HREF="http://welcome.to/scottweb">http://welcome.to/scottweb</A> - The Ultimate Programmers Reference
-
September 26th, 1999, 07:12 AM
#4
Re: Class Inclusion Confusion
You're right - but it will compile if you change the ClassB::classa member to a pointer.
Anyway u can't hold an object of type A inside type B and an object of type B inside A, because it will take infinite memory.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|