C ++ Returning declaration correctly with complex dependencies
Problem
I am getting the following compilation error:
error: member access to incomplete type "IRB"
This happens on the first line that is class IRB
used in HG.h
.
Questions
- What am I missing here? Is it a typedef issue that is causing the problem?
-
In each header file, as shown below, I declare the class that I am going to use.
-
More importantly, what process should I follow to get this right?
Header files
Th
typedef IRBBase__<true, true> IRB_BASE; // typedef a template instantiation
IRB.h
#include "T.h"
#include "FH.h"
class FH; // FWD DCLR
class IRB : IRB_BASE {..};
FH.h
#include "T.h"
#include "IRB.h"
#include "HG.h"
class IRB; // FWD DCLR
class HG; // FWD DCLR
class FH {..};
HG.h
#include "T.h"
#include "FH.h"
#include "IRB.h"
#include "CU.h"
class FH; // FWD DCLR
class IRB; // FWD DCLR
class CU; // FWD DCLR
class HG {..};
CU.h
#include "T.h"
#include "IRB.h"
#include "FH.h"
class IRB;
class FH;
class CU {..};
Edit: it works
Following user0042's suggestion, I got it by moving the headers from all files except HG.h
the corresponding files .cc
. In HG.h
I saved the front ads and header files.
source to share
This answer is based on the comments I received.
Function declarations in header files can use a class that we won't include, but this can cause bike hangs.
So the solution is:
- to move the
#include
classes mentioned in the header file to the original file - in the header file execute the
forward declaration
class
This can be done for any classes that are not used in the header. If we use a header like in the following example HG.h
, we must save #include
in the header file.
Solution of the considered example
Th
typedef IRBBase__<true, true> IRB_BASE; // typedef a template instantiation
IRB.h
class FH; // FWD DCLR
class IRB : IRB_BASE {..};
FH.h
class IRB; // FWD DCLR
class HG; // FWD DCLR
class FH {..};
HG.h
#include "T.h"
#include "FH.h"
#include "IRB.h"
#include "CU.h"
class FH; // FWD DCLR
class IRB; // FWD DCLR
class CU; // FWD DCLR
class HG {..
// some of the classes are used here, because some functions
// were too small and kept them in the header file (defined the function)
// if the same had happened with another of the headers, causing a
// cyclic include dependency, then I should have converted
// some of the function definitions into declarations, and move the
// implementation of the file in the source file (.cc)
};
CU.h
class IRB;
class FH;
class CU {..};
source to share