.h文件的问题 [英] problems with .h files

查看:47
本文介绍了.h文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么

int k;不能在.h文件中定义?


什么是可以/不能放在.h文件中的一般规则。常见问题

似乎无法解决这个问题。

can someone explain why
int k; canNOT be definied in .h file?

what are general rules for what can/cannot be placed in .h file. FAQ
doesn''t seem to address this problem.

推荐答案

参见Lakos; - )

See Lakos ;-)




davidru ... @warpmail.net写道:

davidru...@warpmail.net wrote:
参见Lakos; - )
See Lakos ;-)




我不久前接受过他的采访..也就是忘了问这个问题。
这个问题。但是私人咨询他可能很贵,

我会假设!


建议更多的内容在这个小组的范围内或者球场我

通过解释。

感谢



I have been interviewed by him not so long ago.. i.e. forgot to ask
this question. but he is probably expensive for private consultations,
I would assume!

suggest something more within the scope of this group or ballpark me
through an explanation.
thanks


puzzlecracker写道:
puzzlecracker wrote:
有人可以解释为什么

int k;不能在.h文件中定义?


头文件的目的是将'#include'加入几个

翻译单元。如果你在头文件中放置一个带有外部

链接的对象的定义,那么你迟早会在程序中得到这个对象的几个

定义。这违反了ODR

(一个定义规则),通常会导致编译错误(在

链接阶段)。这就是为什么这样的定义不能放入标题

文件。

什么是能够/不能放在.h文件中的一般规则。
can someone explain why

int k; canNOT be definied in .h file?
The purpose of header files is to be ''#include''d into several
translation units. If you place a definition of an object with external
linkage in header file, you''ll sooner or later end up with several
definitions of this object in the program. This is a violation of ODR
(One Definition Rule) normally resulting in compilation error (at
linking stage). That''s why such definition cannot be placed into header
file.
what are general rules for what can/cannot be placed in .h file.




H文件用于非定义声明和某些类型的
定义。即,类定义,模板定义,内联

函数定义,typename定义可以/应该放在

头文件中。具有静态链接的实体也可以在标题

文件中定义,如果由于某种原因你需要这样做。其他所有内容(除非我错过了某些东西)只应该在

头文件中_declared_(而不是_defined_)。


-

祝你好运,

Andrey Tarasevich



H-files are for non-defining declarations and for certain kinds of
definitions. Namely, class definitions, template definitions, inline
function definitions, typename definitions can/should be placed into
header files. Entities with static linkage can also be defined in header
files, if for some reason you need to do so. Everything else (unless I
missed something) should only be _declared_ (as opposed to _defined_) in
header files.

--
Best regards,
Andrey Tarasevich


这篇关于.h文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆