本文介绍了头文件中没有对象定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stanley B Lippman在他的C ++ Primer中这样的定义

不应该出现在头文件中:


int ix;


包含在两个或多个

相同程序的文件中的任何这些定义将导致链接器错误抱怨多个

定义。因此,应尽可能避免这种定义。

。但是正如我们所知,类的定义总是在一个

头文件中。我们可以使用#ifndef消除链接器错误

抱怨多个定义。


你同意Lippman先生的意见吗?我认为对象的定义

与头文件类的定义相同。多个

定义会导致两者都出错。

Stanley B Lippman in his "C++ Primer" that a definition like this
should not appear in a header file:

int ix;

The inclusion of any of these definitions in two or more files of the
same program will result in a linker error complaining about multiple
definitions. So this kind of definition should be avoided as much as
possible. But as we know, the definition of a class is always in a
header file. And we can use "#ifndef" to eliminate the linker error
complaining about multiple definitions.

Do you agree with Mr Lippman? I thought that the definition of a object
is the same with the definition of a class for header files. Multiple
definitions will cause error for both of them.

推荐答案



你不必同意。亲自尝试一下。

You don''t have to agree. Just try it yourself.



否。我们所知道的类别定义仅包含声明

和内联函数的定义。如果在程序中多次出现

,则完全合法。对象定义不同。


V

-

请在回复e-时删除资金'A'邮件

我没有回复最热门的回复,请不要问

No. What we know as "class definitions" consist only of declarations
and definitions of inline functions. All perfectly legal if appear
multiple times in a program. Object definitions are different.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




你的信念很自然,是C ++模糊或模糊的结果

类型和变量之间的区别。类定义是*类型*的

定义,而不是变量。是的,他是对的。

Your belief is kind of natural and is a result of C++ blurring or obscuring
the distinction between types and variables. A class definition is the
definition of a *type*, not a variable. Yes, he''s right.




你不必同意。亲自尝试一下。


You don''t have to agree. Just try it yourself.



编号我们所知道的类别定义仅包含声明

和内联函数的定义。如果在程序中多次出现

,则完全合法。对象定义不同。


No. What we know as "class definitions" consist only of declarations
and definitions of inline functions. All perfectly legal if appear
multiple times in a program. Object definitions are different.



内联函数的声明和定义在程序中可以出现多次

次而不会导致错误。确实如此。但是如果

类定义整体出现多次呢?我认为

会导致错误。


BTW,我一直认为声明应该放入头文件中,

和定义应该放在源文件中。但是我们使用
来将类定义放入头文件这一事实让我感到困惑。

Declarations and definitions of inline functions can appear multiple
times in a program without causing errors. That''s true. But what if the
class definition as a whole appear multiple times? I thought that that
will cause error.

BTW, I always think that declarations should be put into header files,
and definitions should be put into source files. But the fact that we
are used to put class definitions into header files made me confused.


这篇关于头文件中没有对象定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:42