当前位置:网站首页>Header file duplicate definition problem solving "c1014 error“
Header file duplicate definition problem solving "c1014 error“
2022-07-04 23:07:00 【Programming segment】
For example, now there are three files , Two header files , One .cpp file
header1.h
#include "header2.h"
int fun2();
header2.h
#include "header1.h"
int fun();
main.cpp
#include "header1.h"
int main()
{
return 0;
}
The compiler will replace the header file in the preprocessing process , Finally, the following code is formed , The header file 1 Include header file 2, The header file 2 Also includes header file 1, Nesting with each other , This is the compiler will report an error “C1014 Contains too many files : depth = 1024 ”
#include "header1.h"
...
int fun();
int fun2();
int main()
{
return 0;
}
There are two solutions :
- One is
#ifndef
solve
But this name is customized , There may be the same time , For example, two files are written at the same timeHEADER_1
, This will reduce the number of header files , Therefore, it is suggested to adopt the second
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;
}
After the header file is expanded
#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;
}
- The other is to adopt
#pragma once
The principle of this implementation is , There will be a count inside , Indicates that this header file will only be expanded 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;
}
边栏推荐
- Explanation of bitwise operators
- How to choose a securities company? Is it safe to open an account on your mobile phone
- JS card style countdown days
- 推荐收藏:跨云数据仓库(data warehouse)环境搭建,这货特别干!
- [try to hack] wide byte injection
- P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
- [machine learning] handwritten digit recognition
- ffmpeg快速剪辑
- Qt加法计算器(简单案例)
- Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
猜你喜欢
随机推荐
cout/cerr/clog的区别
VIM editor knowledge summary
How to choose a securities company? Is it safe to open an account on your mobile phone
【剑指offer】1-5题
Redis introduction complete tutorial: detailed explanation of ordered collection
Install the gold warehouse database of NPC
为什么信息图会帮助你的SEO
LabVIEW中比较两个VI
JS card style countdown days
微信公众号解决从自定义菜单进入的缓存问题
Redis入门完整教程:慢查询分析
Three stage operations in the attack and defense drill of the blue team
Redis introduction complete tutorial: slow query analysis
Redis入门完整教程:GEO
A complete tutorial for getting started with redis: hyperloglog
Redis démarrer le tutoriel complet: Pipeline
[ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
String类中的常用方法
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
Analysis of the self increasing and self decreasing of C language function parameters