是否有可能在A&QUOT需要#include;菱形继承"结构体? [英] Is it possible to #Include in a "diamond inheritance" structure?
本文介绍了是否有可能在A&QUOT需要#include;菱形继承"结构体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图做一些C项目。
我想知道是否有可能从同一个文件让的#include
两次,在回忆钻石传承的一种方式。
即
- 在交流转换器有
的#includeA.H
- 在 b.c 有
的#includeb.h
- 在 b.h 有
的#includeA.H
是否有可能的#includeb.h
在交流转换器
我得到一个错误:
some_variable已经A.OBJ定义
解决方案
简单:没有的确定的头文件中的变量,只是声明的他们:
标题:
// A.H的#ifndef A_H //经常使用的#include卫士
#定义A_HEXTERN INT my_variable; //声明my_variable...#万一
来源文件交流转换器:
//交流转换器#包括A.HINT my_variable; //定义my_variable...
来源文件b.c:
//交流转换器#包括A.H
#包括b.h...
正如其他人所说,后卫的#include是有用的,和良好的习惯进入,但他们可能不会为这一特定问题的解决方案。
I'm trying to make some project in C.
I would like to know if it is possible to make #include
from the same file twice, in a way that recalls diamond heritage.
i.e.
- in a.c there is
#include "a.h"
- in b.c there is
#include "b.h"
- in b.h there is
#include "a.h"
Is it possible to #include "b.h"
in a.c?
I get an error:
some_variable already defined in a.obj
解决方案
Simple: don't define variables in headers, just declare them:
Header:
// a.h
#ifndef A_H // always use #include guards
#define A_H
extern int my_variable; // declare my_variable
...
#endif
Source file a.c:
// a.c
#include "a.h"
int my_variable; // define my_variable
...
Source file b.c:
// a.c
#include "a.h"
#include "b.h"
...
As others have mentioned, #include guards are useful, and a good habit to get into, but they are probably not the solution for this specific problem.
这篇关于是否有可能在A&QUOT需要#include;菱形继承"结构体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文