包含头文件C ++的问题 [英] The problem with including header files c++

查看:86
本文介绍了包含头文件C ++的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中有3个头文件:Form1.h-这是带有实现的头文件,TaskModel.h和TaskModel.cpp,TaskController.h和TaskController.cpp.

有文件内容:

//-----
TaskController.h

#pragma一旦
#include"TaskModel.h"
..........



//----
Form1.h
#pragma一旦
#include"TaskModel.h"
#include"TaskController.h"
.........



问题:

如何使Form1.h包含在TaskModel.h中.当我直接将Form1.h包含到TaskModel.h中时,就会出现很多错误.如果要使用前向声明,如何组织该声明?

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 ?

推荐答案

如果前者已经包含在Form1.h中,则不必在TaskModel.h中包括它. .您所描述的交叉依赖关系过多,表明您的设计可能需要进行一些重组.但是,由于没有关于每个头文件中定义的内容的进一步信息,很难建议如何最好地解决此问题.
You should not need to include Form1.h in TaskModel.h when the latter is already include in the former. Too many cross dependencies such as you describe are an indication that your design may need to be restructured somewhat. However, without further information as to what is defined in each header file it is difficult to suggest how best to correct the problem.


我已经做到了:

I''ve done it:

// in TaskModel.h 
 
class Form1; // or other classes that are using in TaskModel.h 
 
//... task model code 
 
// in TaskModel.cpp 
 
#include "Form1.h"


这篇关于包含头文件C ++的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆