当前位置:网站首页>头文件重复定义问题解决“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;
}
边栏推荐
- 小程序vant tab组件解决文字过多显示不全的问题
- Redis introduction complete tutorial: client communication protocol
- 【二叉树】节点与其祖先之间的最大差值
- Redis introduction complete tutorial: slow query analysis
- SHP data making 3dfiles white film
- Summary of index operations in mongodb
- Explanation of bitwise operators
- 【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
- Photoshop批量给不同的图片添加不同的编号
- 【图论】拓扑排序
猜你喜欢

A complete tutorial for getting started with redis: transactions and Lua

攻防世界 MISC 进阶区 3-11

OSEK标准ISO_17356汇总介绍

PS style JS webpage graffiti board plug-in

Serial port data frame

MySQL Architecture - user rights and management

Redis introduction complete tutorial: List explanation

攻防世界 MISC 進階區 Erik-Baleog-and-Olaf

vim编辑器知识总结
![[OpenGL] note 29 anti aliasing (MSAA)](/img/66/61f29e1c41d3099d55e2ead0a3b01e.png)
[OpenGL] note 29 anti aliasing (MSAA)
随机推荐
【二叉树】节点与其祖先之间的最大差值
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
Redis的持久化机制
LabVIEW中比较两个VI
Notepad++--编辑的技巧
Redis getting started complete tutorial: publish and subscribe
Redis入门完整教程:有序集合详解
Redis introduction complete tutorial: detailed explanation of ordered collection
ECS settings SSH key login
攻防世界 MISC 进阶区 3-11
The difference between Max and greatest in SQL
D3.js+Three. JS data visualization 3D Earth JS special effect
环境加密技术解析
Redis入门完整教程:发布订阅
On-off and on-off of quality system construction
【剑指Offer】6-10题
[machine learning] handwritten digit recognition