Forward declaring
I''m back with another question.
The problem now is that I wan''t to forward declare a class but I''m being unsuccesfull right now.
In one header I define a class AA.
In another header I have class BB which has a private member of type AA. I read the Reducing code dependencies tutorial at flipcode.com and they said you should forward declare classes.
So in the header that class BB is defined I add this:
class AA;
The member variable of type AA in class BB is defined like this:
AA aa;
I get this error on that last line:
''aa'' uses undefined class ''AA''.
What am I doing wrong this time?
Try sticking extern in front of the class definition like so:
extern Class AA;
extern Class AA;
When I find my code in tons of trouble,Friends and colleages come to me,Speaking words of wisdom:"Write in C."My Web Site
The problem is that if you want a member of type AA, the compiler needs to know the size of it so it knows how big BB will be. So you''ll have to included the header for AA. If you store a pointer to AA, then you don''t need the size (because all pointers are the same size) then you just new it in the constructor of AA, and delete it in the destructor.
Header1.h has AA
Header2.h has BB, but it needs Header1.h
Solution 1 would be to include Header1.h in Header2.h and then only include Header2.h in your main source file. If however classes defined in Header2.h are needed in Header1.h and visa-versa, then you going to have to use processor directives:
// Header1.h ------
#ifndef HEADER1_INCLUDED
#define HEADER1_INCLUDED
#include "Header2.h"
...
#endif
// ----------------
// Header2.h ------
#ifndef HEADER2_INCLUDED
#define HEADER2_INCLUDED
#include "Header1.h"
...
#endif
// ----------------
Then include either header in your main program. That should work without errors.
Header2.h has BB, but it needs Header1.h
Solution 1 would be to include Header1.h in Header2.h and then only include Header2.h in your main source file. If however classes defined in Header2.h are needed in Header1.h and visa-versa, then you going to have to use processor directives:
// Header1.h ------
#ifndef HEADER1_INCLUDED
#define HEADER1_INCLUDED
#include "Header2.h"
...
#endif
// ----------------
// Header2.h ------
#ifndef HEADER2_INCLUDED
#define HEADER2_INCLUDED
#include "Header1.h"
...
#endif
// ----------------
Then include either header in your main program. That should work without errors.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement