I have few different classes. Few pairs need to know about each other. To prevent the headache I've created one file to keep forward declarations of all troublesome classes along with proper include order.
#ifndef GLPROJECT_H
#define GLPROJECT_H
class MainWindow;
class Scene;
class ShaderProgram;
class Shape;
#include "ShaderProgram.h"
#include "MainWindow.h"
#include "Shape.h"
#include "Scene.h"
#endif
Every file who needs another one from the given set include this header. I came up with an idea to put all includes inside include guard, so for example Shape.h
class looks like:
#ifndef SHAPE_H
#define SHAPE_H
#include "GLProject.h" //file above
//...class definition code
#endif
However this example produces error: field ‘x’ has incomplete type ‘GLProject::Shape’
in file Scene.h
, which is stated after the Shape.h
in the main header (other files don't include Scene.h
explicitly).
(Note that following flow is correct only for files which directly include GLProject.h
)
If I'd trace the inclusion of files starting with GLProject.h
, then:
1)
- it includes the first header which doesn't include any of these headers,
- MainWindow which include GLProject.h but it's completely omited due to include guards,
Shape.h
which definesShape.h
(after trying to include guardedGLProject.h
)Scene.h
which declares viariable of typeShape
that should be already defined.
So no idea why does it complains that Shape
is incomplete type.
What's more interesting moving inclusion of file
GLProject.h
in fileShape.h
above include guards fixes the problem. (the most important fact)