添加包含防护会破坏构建
我将 #ifndef..#define..#endif 添加到我的项目文件中,但编译器失败。一旦我删除它或在定义中放入任何其他名称,它就可以正常编译。可能是什么问题?
听起来该文件已经声明了,但我不知道在哪里。我只是删除它就可以了,但我真的想知道为什么会发生这种情况。
error: expected class-name before ‘{’ token
error: ‘QDesignerFormEditorInterface’ has not been declared
还有其他一些错误。
我实际上使用的是 Qt 中的示例“自定义小部件插件示例”。
不同之处在于我使用自己的类作为自定义小部件(.h、.cpp 和 .ui 文件)。
它可能与具有 2 个包含的文件有关,尽管示例就是这样做的。
I added #ifndef..#define..#endif to a file of my project and the compiler fails. As soon as I remove it or put any other name in the define it compiles fine. What could be the problem?
Sounds like the file is already declared, but I do not know where. I'm fine just removing it, but I really want to know why this is happening.
error: expected class-name before ‘{’ token
error: ‘QDesignerFormEditorInterface’ has not been declared
And a couple of other errors.
I am actually using an example from Qt, "Custom Widget Plugin Example".
The difference is I am using my own class for the custom widget (.h, .cpp and .ui file).
It might have to do with the file that has 2 includes, though that is how the example did it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该宏是否用作包含防护?如果是这样,听起来您正在重复其他地方使用的名称。当人们没有考虑包含防护必须具有的范围时,这是一个常见问题 - 您应该在其中包含更多信息,而不仅仅是文件名。
包含守卫目标:
错误的包含守卫名称(对于文件“config.h”):
CONFIG_H
_CONFIG_H
、CONFIG__H
、CONFIG_H__
、__CONFIG_H__
等。PROJECT_CONFIG_H
良好的包含保护名称(对于文件“config.h”):
PATE_20091116_142045
<姓氏>_<日期>_<时间>
INCLUDE_GUARD_726F6B522BAA40A0B7F73C380AD37E6B
Is this macro used as an include guard? If so, it sounds like you're duplicating a name used elsewhere. This is a common problem when people don't think about the scope an include guard must have—you should include much more information in it than just the file name.
Include guard goals:
Bad include guard names (for file "config.h"):
CONFIG_H
_CONFIG_H
,CONFIG__H
,CONFIG_H__
,__CONFIG_H__
, etc.PROJECT_CONFIG_H
Good include guard names (for file "config.h"):
PATE_20091116_142045
<last name>_<date>_<time>
INCLUDE_GUARD_726F6B522BAA40A0B7F73C380AD37E6B
如果为已定义的常量添加
#ifndef
,则它将始终验证为 true。您说“文件已声明”,但文件并未声明。实际上,您应该检查的是放置在#ifndef
之后的常量。对整个源代码树进行简单搜索,并仔细检查当前#define
出现的顺序。当然:你的代码正确吗?尝试使用垃圾名称作为常量,并将
#endif
放在它后面:如果仍然错误,则说明您有拼写错误(如果是,请粘贴您的代码)。另请参阅这篇文章。PS:我看到 David Thornley 在评论中输入了类似的建议...抱歉,如果这与本线程中的信息重复。
If you add an
#ifndef
for a constant that's already defined, it will always validate to true. You say "the file is declared", but files do not get declared. It is really the constant that you place after#ifndef
that you should be checking. Do a simple search through the whole source tree and double-check in what order your current#define
appears.And of course: is your code correct? Try with a rubbish name as constant, and place
#endif
right after it: if it still errors, you have typos (paste your code, if so). See also this post.PS: I see that David Thornley was typing similar advice in a comment... sorry if this duplicates info in this thread.