多个头文件的简单管理

common.h一开始是这样的:

#ifndef _common_H
#define _common_H

#include "iostream"
using namespace std;
#include "string"

#include "headF.h"
#include "headS.h"
#include "headT.h"
#include "headV.h"

#endif


headF.h:

#ifndef _headF_H
#define _headF_H

#include "common.h"

class headF
{
public:
	headF()
	{
		age = 0;
		name = "headF";
	}
	void ShowF()
	{
		cout << "name : " << name << endl;
	}

private:
	int age;
	string name;
};

#endif


headS.h:

#ifndef _headS_H
#define _headS_H

#include "common.h"

class headS
{
public:
	headS()
	{
		age = 0;
		name = "headS";
	}
	void ShowF()
	{
		cout << "name : " << name << endl;
	}

private:
	int age;
	string name;
};

#endif


headT.h:

#ifndef _headT_H
#define _headT_H

#include "common.h"
#include "headV.h"

class headT
{
public:
	headT(int age, string name)
	{
		_age = age;
		_name = name;
	}
	void ShowF()
	{
		cout << "name : " << _name << endl;
	}

private:
	int _age;
	string _name;
};

#endif


headV.h:

#ifndef _headV_H
#define _headV_H

#include "common.h"
#include "headT.h"

class headV
{
public:
	headV()
	{
		_age = 0;
		_name = "headV";
		test = new headT(99, "headV");
	}
	void ShowF()
	{
		cout << "name : " << _name << endl;
		test->ShowF();
	}

private:
	int _age;
	string _name;
	headT *test;
};


#endif


但是这时出现了一个问题,

1>------ 已启动生成: 项目: ConsoleApplication1, 配置: Debug Win32 ------
1>  ConsoleApplication1.cpp
1>c:\users\lenovo\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\headv.h(25): error C2143: 语法错误 : 缺少“;”(在“*”的前面)
1>c:\users\lenovo\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\headv.h(25): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\users\lenovo\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\headv.h(14): error C2065: “test”: 未声明的标识符
1>c:\users\lenovo\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\headv.h(14): error C2061: 语法错误: 标识符“headT”
1>c:\users\lenovo\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\headv.h(19): error C2065: “test”: 未声明的标识符
1>c:\users\lenovo\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\headv.h(19): error C2227: “->ShowF”的左边必须指向类/结构/联合/泛型类型
1>          类型是“'unknown-type'”
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

 

那是因为在headV中再也找不到headT的定义了

 

分析一下原因:

因为是使用预编译命令

在common.h中包含了:#include "headT.h"、#include "headV.h"

在headT.h中又包含了headV.h,headV.h中也包含了headT.h

这就造成了,上面的原因

 

所以对应,把common.h修改成以下:

#ifndef _common_H
#define _common_H

#include "iostream"
using namespace std;
#include "string"

#endif


 该问题就解决了,可是一般在大项目中,头文件还是比较难管理的,也是比较容易出现问题的部分。

 

附上一个不正确的头文件关系图:

 

这是正确的关系:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值