重复标题声明 [英] Duplicate Header Declaration

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

问题描述

我决定在K& R中创建一个包含所有重复的

函数的文件,这样我就可以集中精力进行新的讨论了。

这很顺利,并使用每个新函数,将

声明添加到foo.h,将函数添加到foo.c.然后,在一个
汇编中,我突然收到大量的失败和警告。





重新定义错误。错误抱怨没有声明变量
,当它是等等时


最后证明我在标题中复制了一个声明

文件,它解决了所有不同的警告。


我有几个问题。


1)我认为它如果定义的声明比一次更多,那么没有区别吗?

2)对于经验更丰富的C程序员来说,如果一个人突然获得了b
警告和编译失败,这是你b / b
会自动想到的吗?即重复声明。 (它花了我几个小时的时间去掉每个函数/声明几个小时才显示错误是什么)

明显错误是什么。


谢谢

I decided to make a single file containing all the repetitive
functions in K&R so that I could concentrate on the new discussions.
This went along just fine, and with each new function, added the
declaration to foo.h and the function to foo.c. Then, on one
compilation, I suddenly got a massive number of failures and warnings.

Like

"Redefinition errors. errors complaining about a variable not having
been declared, when it was etc"

Finally turns out that I had duplicated a declaration in the header
file, which solved all the disparate warnings.

I have a couple of questions.

1) I thought it made no difference if a definition is declared more
than once?
2) To the more experienced C programmers, if one suddenly gets
widespread warnings and failures to compile, is this something you
would automatically think of? ie a duplicate declaration. ( It took me
a few hours of removing each function/declaration before it was
obvious what the error was).

Thanks

推荐答案

mdh说:
mdh said:

我决定制作一个包含所有文件的文件重复的

在K& R中起作用,这样我就可以专注于新的讨论。
I decided to make a single file containing all the repetitive
functions in K&R so that I could concentrate on the new discussions.



所以,实际上,你正在编写自己的库(或者更确切地说,是K& R'自己的

库)。很好,一件好事,为你做好准备。

So, effectively, you''re writing your own library (or rather, K&R''s own
library). Fine, a good thing to do, good practice for you.


这很顺利,每个新功能都添加了

对foo.h的声明和foo.c的函数。
This went along just fine, and with each new function, added the
declaration to foo.h and the function to foo.c.



正确。

Right.


然后,在一个

编译中,我突然得到了大量的失败和警告。
Then, on one
compilation, I suddenly got a massive number of failures and warnings.



嗯。

Hmmm.





"重新定义错误。错误抱怨没有声明变量
,当它是等等时
Like

"Redefinition errors. errors complaining about a variable not having
been declared, when it was etc"



调试一个释义很难。

It''s hard to debug a paraphrase.


最后证明我有重复的一个标题中的声明

文件,它解决了所有不同的警告。
Finally turns out that I had duplicated a declaration in the header
file, which solved all the disparate warnings.



原谅我,但这听起来不太可能。听起来更可能的是

你以某种方式复制了一些函数定义。

Forgive me, but this sounds unlikely. What sounds more likely is that
you''ve somehow duplicated some function definitions.


我有几个问题。


1)我认为如果宣布的定义比一次更多,那么它没有区别吗?
I have a couple of questions.

1) I thought it made no difference if a definition is declared more
than once?



问题不清楚。你甚至不能宣布一个定义,所以你

肯定不能多次声明它。你声明的东西是

对象和函数。需要小心,因为虽然多个

声明*是好的,多个定义都不行,所有对象

和函数定义本身就是声明,所以它是'容易混合

声明定义。

The question is unclear. You can''t declare a definition even once, so you
certainly can''t declare it more than once. The things you declare are
objects and functions. Care is needed, because although multiple
declarations *are* okay, multiple definitions are not okay, and all object
and function definitions are themselves declarations, so it''s easy to mix
up declarations with definitions.


2)对于经验丰富的C程序员,如果突然得到

广泛的警告和编译失败,这是你b / b
会自动想到的吗?
2) To the more experienced C programmers, if one suddenly gets
widespread warnings and failures to compile, is this something you
would automatically think of?



经验丰富的C程序员几乎肯定会对这样一个问题做出回应/ b / b
他们甚至可能都没有注意到他们是响应。

Experienced C programmers will almost certainly respond /so/ automatically
to such a problem that they might not even notice that they''re responding.


即重复声明。 (我花了几个小时的时间删除了每个函数/声明,然后才明白错误是什么)。
ie a duplicate declaration. ( It took me
a few hours of removing each function/declaration before it was
obvious what the error was).



错误是什么仍然不明显。为了帮助我们,因此你可以更全面地理解错误,重建

错误可能是有用的,也许是在你的代码之后共享你的代码状态

几个小时的调试时间 - 即错误点仍然存在的点b / b
但是突然显而易见。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

It is still not obvious what the error was. To assist in our, and therefore
your, fuller understanding of the error, it might be useful to reconstruct
the error, perhaps by sharing the state of your code after your
few-hours-long debugging session - i.e. the point at which the error was
still present, but suddenly obvious to you.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


mdh< md ** @ comcast.netwrites:
mdh <md**@comcast.netwrites:

我决定制作一个包含K& R中所有重复

函数的文件,以便我可以集中精力进行新的讨论。

这很好,并且每个新功能都有,将

声明添加到foo.h,将函数添加到foo.c.然后,在一个
编译中,我突然遇到了大量的失败和警告。
I decided to make a single file containing all the repetitive
functions in K&R so that I could concentrate on the new discussions.
This went along just fine, and with each new function, added the
declaration to foo.h and the function to foo.c. Then, on one
compilation, I suddenly got a massive number of failures and warnings.



[...]

[...]


(删除每个函数/声明花了我几个小时

之前很明显错误是什么)。
( It took me a few hours of removing each function/declaration
before it was obvious what the error was).



这是偏离主题的,但是......


这是源代码控制系统得到回报的地方。如果您保存了源文件的每个修订版本的

记录,那么只要您开始收到编译错误,就可以比较当前的

版本对抗最后一个有效并且看到*确切*你改变了什么
。或者,如果你无法弄明白,你可以恢复到之前版本的

并从那里重新开始,希望不要再次犯同样的错误。


经常检查您的更改非常重要。一个体面的系统

将只保存差异,因此保存100个版本的文件不会像单个副本那样花费近100倍的磁盘空间。


对于类Unix系统,RCS和CVS都很好。对于其他系统有各种各样的

解决方案(大多数我熟悉的解决方案是
Windows是从Unix移植还是昂贵的)。或者你可以只用

保留旧版本文件的副本。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

This is mildly off-topic, but ...

This is where source code control systems pay off. If you kept a
record of each revision of your source files, the as soon as you
started getting compilation errors, you could compare the current
version against the last one that worked and see *exactly* what you
changed. Or, if you can''t figure it out, you can revert to the
previous version and start again from there, hoping not to make the
same mistake again.

It''s important to check in your changes frequently. A decent system
will save only the differences, so saving 100 versions of a file won''t
cost nearly 100 times as much disk space as a single copy.

For Unix-like systems, RCS and CVS are good. There are various
solutions for other systems (most of the ones I''m familiar with for
Windows are either ported from Unix or expensive). Or you can just
keep copies of old versions of your files.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


mdh写道:
mdh wrote:

我决定制作一个单独的文件,其中包含K& R中所有重复的

函数,以便我可以专注于新的讨论。 />
这很好,并且每个新函数都将

声明添加到foo.h并将函数添加到foo.c.然后,在一个
汇编中,我突然收到大量的失败和警告。





重新定义错误。错误抱怨没有声明变量
,当它是等等时


最后证明我在标题中复制了一个声明

文件,它解决了所有不同的警告。


我有几个问题。


1)我认为它如果定义的声明比一次更多,那么没有区别吗?
I decided to make a single file containing all the repetitive
functions in K&R so that I could concentrate on the new discussions.
This went along just fine, and with each new function, added the
declaration to foo.h and the function to foo.c. Then, on one
compilation, I suddenly got a massive number of failures and warnings.

Like

"Redefinition errors. errors complaining about a variable not having
been declared, when it was etc"

Finally turns out that I had duplicated a declaration in the header
file, which solved all the disparate warnings.

I have a couple of questions.

1) I thought it made no difference if a definition is declared more
than once?



令人惊讶的是,我昨天才遇到类似的问题。


我有一个结构和一个由多个函数使用的枚举在

不同的文件。

所以我在头文件中写了struct和enum的声明

foo.h并将其包含在需要它们的文件。然后编译

kaboom!我有68个错误和警告非常类似于你获得的b / b
。我已经重新定义了 enum不止一次。最简单的

解决方案是:


#ifndef FOO_H

#define FOO_H
....

#endif

Amazingly I faced a similar issue only yesterday .

I had a struct and an enum which is used by several functions in
different files.
So i wrote the declaration of the struct and the enum in a header file
foo.h and included it in the files that needed them.Then on compiling
kaboom! I have some 68 errors and warnings very similar to those you
got. I Had re-"defined" enum more than once it seems.And the simplest
solution was :

#ifndef FOO_H
#define FOO_H
....
#endif


2)对于经验丰富的C程序员,如果有的话突然得到

广泛的警告和编译失败,这是你b / b
会自动想到的吗?即重复声明。 (它花了我几个小时的时间去掉每个函数/声明几个小时才显示错误是什么)

明显错误是什么。


谢谢
2) To the more experienced C programmers, if one suddenly gets
widespread warnings and failures to compile, is this something you
would automatically think of? ie a duplicate declaration. ( It took me
a few hours of removing each function/declaration before it was
obvious what the error was).

Thanks


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

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