当前位置:网站首页>头文件重复定义问题解决“C1014错误“
头文件重复定义问题解决“C1014错误“
2022-07-04 22:37:00 【编程小段】
比如现在有三个文件,两个头文件,一个.cpp文件
header1.h
#include "header2.h"
int fun2();
header2.h
#include "header1.h"
int fun();
main.cpp
#include "header1.h"
int main()
{
return 0;
}
编译器在预处理过程会进行头文件替换,最终形成以下代码,头文件1包含头文件2,头文件2又包含头文件1,互相嵌套,这是编译器就会报报错“C1014 包含文件太多: 深度 = 1024 ”
#include "header1.h"
...
int fun();
int fun2();
int main()
{
return 0;
}
解决办法有两种:
- 一种是
#ifndef解决
但这种名字是自定义的,可能会存在相同的时候,例如两个文件同时写成了HEADER_1,这样就会少包含一组头文件,所以建议采用第二种
header1.h
#ifndef HEADER_1
#define HEADER_1
#include "header2.h"
int fun2();
#endif
header2.h
#ifndef HEADER_2
#define HEADER_2
#include "header1.h"
int fun();
#endif
main.cpp
#include "header1.h"
int main()
{
return 0;
}
经过头文件展开后
#define HEADER_1
#define HEADER_2
#ifndef HEADER_1
#define HEADER_1
#include "header2.h"
int fun2();
#endif
int fun();
int fun2();
int main()
{
return 0;
}
- 另一种是采用
#pragma once
这样实现的原理是,内部会有一个计数,表示这个头文件只会被展开一次
header1.h
#pragma once
#include "header2.h"
int fun2();
header2.h
#pragma once
#include "header1.h"
int fun();
main.cpp
#include "header1.h"
int main()
{
return 0;
}
边栏推荐
- Redis getting started complete tutorial: Key Management
- Attack and defense world misc advanced area ditf
- 字体设计符号组合多功能微信小程序源码
- 页面关闭前,如何发送一个可靠请求
- A complete tutorial for getting started with redis: redis usage scenarios
- Complete tutorial for getting started with redis: bitmaps
- Serial port data frame
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- Redis入门完整教程:API的理解和使用
- 为什么信息图会帮助你的SEO
猜你喜欢

Redis introduction complete tutorial: slow query analysis

Explanation of bitwise operators

Redis: redis configuration file related configuration and redis persistence

Install the gold warehouse database of NPC

LabVIEW中比较两个VI

Unity vscode emmylua configuration error resolution

Attack and defense world misc advanced area can_ has_ stdio?

Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click

Redis入门完整教程:初识Redis
页面关闭前,如何发送一个可靠请求
随机推荐
Mongodb aggregation operation summary
Persistence mechanism of redis
LabVIEW中比较两个VI
MYSQL架构——逻辑架构
Advanced area of attack and defense world misc 3-11
ECS settings SSH key login
Summary of index operations in mongodb
Redis入门完整教程:客户端通信协议
Google collab trample pit
MySQL Architecture - logical architecture
On-off and on-off of quality system construction
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
Redis introduction complete tutorial: detailed explanation of ordered collection
【剑指Offer】6-10题
企业如何跨越数字化鸿沟?尽在云原生2.0
Attack and defense world misc advanced grace-50
攻防世界 misc 高手进阶区 a_good_idea
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
Attack and defense world misc advanced area ditf
[Jianzhi offer] 6-10 questions