I have 3 header files in the project: Form1.h – this is header with implementation there, TaskModel.h with TaskModel.cpp, TaskController.h with TaskController.cpp.
There are content of files:
//-----
TaskController.h
#pragma once
#include "TaskModel.h"
..........
//----
Form1.h
#pragma once
#include "TaskModel.h"
#include "TaskController.h"
.........
The problem:
How to make Form1.h to be included to TaskModel.h. When I directly include, Form1.h to TaskModel.h then there are many errors. If to use forward declaration, how to organaize that ?
You can forward declare classes not header files.
The problem with cyclic dependencies is usually a mark of bad design. Do you want TaskModel.h to include Form1.h? Why is that? Can it be avoided? Couldn’t you just include Form1.h into TaskModel.cpp?
For forward declaration do:
Basically what you are doing here is declaring that such classes exist. Then in the cpp file you include them.
Mind however that this has some limitations:
As a rule of thumb, if the forwarded classes size is needed to compile the given piece of code, you cannot use a forward.