Scott MacMaster
September 25th, 1999, 11:03 PM
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
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