当前位置:网站首页>头文件重复定义问题解决“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;
}
边栏推荐
- PS style JS webpage graffiti board plug-in
- colResizable. JS auto adjust table width plug-in
- Redis入门完整教程:Redis Shell
- 攻防世界 MISC 进阶区 hong
- Redis: redis configuration file related configuration and redis persistence
- Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
- 堆排序代码详解
- 该如何去选择证券公司,手机上开户安不安全
- On-off and on-off of quality system construction
- Redis入门完整教程:API的理解和使用
猜你喜欢
![[machine learning] handwritten digit recognition](/img/26/cabdc5c92035181d82f6f809e6df0f.png)
[machine learning] handwritten digit recognition

攻防世界 MISC 进阶区 can_has_stdio?
页面关闭前,如何发送一个可靠请求

新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码

Redis introduction complete tutorial: List explanation

Redis introduction complete tutorial: client communication protocol

Attack and defense world misc advanced area ditf

The overview and definition of clusters can be seen at a glance

vim编辑器知识总结

Redis入门完整教程:发布订阅
随机推荐
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
Talk about Middleware
攻防世界 MISC 进阶区 Ditf
【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
Lost in the lock world of MySQL
A complete tutorial for getting started with redis: transactions and Lua
Persistence mechanism of redis
Duplicate ADMAS part name
On-off and on-off of quality system construction
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
UML diagram memory skills
攻防世界 misc 高手进阶区 a_good_idea
Redis getting started complete tutorial: publish and subscribe
Sword finger offer 67 Convert a string to an integer
页面关闭前,如何发送一个可靠请求
Photoshop batch adds different numbers to different pictures
Common methods in string class
Redis入門完整教程:Pipeline
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
Async await used in map