当前位置:网站首页>头文件重复定义问题解决“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;
}
边栏推荐
猜你喜欢
Attack and defense world misc master advanced zone 001 normal_ png
MYSQL架构——逻辑架构
Explanation of bitwise operators
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 grace-50
Attack and defense world misc advanced area can_ has_ stdio?
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
Redis入门完整教程:GEO
[sword finger offer] questions 1-5
堆排序代码详解
随机推荐
How to choose a securities company? Is it safe to open an account on your mobile phone
Attack and defense world misc advanced area Hong
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
堆排序代码详解
The overview and definition of clusters can be seen at a glance
Attack and defense world misc advanced area can_ has_ stdio?
Redis入门完整教程:慢查询分析
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
Sobel filter
JS card style countdown days
Redis introduction complete tutorial: detailed explanation of ordered collection
Pagoda 7.9.2 pagoda control panel bypasses mobile phone binding authentication bypasses official authentication
Erik baleog and Olaf, advanced area of misc in the attack and defense world
Redis入门完整教程:客户端通信协议
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
The solution to the lack of pcntl extension under MAMP, fatal error: call to undefined function pcntl_ signal()
Redis入门完整教程:Redis Shell
Photoshop batch adds different numbers to different pictures
Summary of wechat applet display style knowledge points
Notepad++--编辑的技巧