Reputation: 346
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 ?
Upvotes: 1
Views: 428
Reputation: 57525
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:
// in TaskModel.h
class Form1; // or other classes that are using in TaskModel.h
//... task model code
// in TaskModel.cpp
#include "Form1.h"
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.
Upvotes: 3
Reputation: 3401
I think you are saying that "TaskModel.h" is being included more than once by your module. To avoid this, at the top of "TaskModel.h" you can put:
#ifndef TASK_MODEL_H
#define TASK_MODEL_H
then at the end of the file put:
#endif
Upvotes: -1