添加包含防护会破坏构建

发布于 2024-08-12 04:41:06 字数 422 浏览 3 评论 0原文

我将 #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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聽兲甴掵 2024-08-19 04:41:06

该宏是否用作包含防护?如果是这样,听起来您正在重复其他地方使用的名称。当人们没有考虑包含防护必须具有的范围时,这是一个常见问题 - 您应该在其中包含更多信息,而不仅仅是文件名。

包含守卫目标:

  • 生成一次,创建标头时
  • 再也不用考虑
  • 重复的机会小于中奖的机会

错误的包含守卫名称(对于文件“config.h”):

  • CONFIG_H
    • 太笼统
  • _CONFIG_HCONFIG__HCONFIG_H____CONFIG_H__ 等。
    • 全部保留,仍然太多一般
  • PROJECT_CONFIG_H
    • 更好,在不相关的项目中重复的可能性更小
    • 但仍然没有路径信息,在大型项目中容易复制

良好的包含保护名称(对于文件“config.h”):

  • PATE_20091116_142045
    • 这是<姓氏>_<日期>_<时间>
    • 甚至不需要项目、路径、文件名信息
    • 易于输入
      • 如果您的编辑器具有插入日期功能,您可以非常快速地“输入”它
    • 易于生成
      • 如果您需要每秒生成多个,请在生成时包含一个序列号
    • 全球独一无二的有力保证
  • INCLUDE_GUARD_726F6B522BAA40A0B7F73C380AD37E6B
    • 从实际的UUID生成
      • 全球独一无二的有力保证
    • 如果它意外出现,“INCLUDE_GUARD”很好地提示了它是什么,同时将其放在单独的命名空间中(尽管按照惯例而不是被语言识别)
    • 如果需要,可以在前面添加项目名称(宏的项目指南通常要求这样做)
    • 轻松编写自己的示例程序来生成< /里>

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:

  • generate once, when creating a header
  • never have to think about again
  • chance of duplicating is less than your chance of winning the lottery

Bad include guard names (for file "config.h"):

  • CONFIG_H
    • much too general
  • _CONFIG_H, CONFIG__H, CONFIG_H__, __CONFIG_H__, etc.
  • PROJECT_CONFIG_H
    • better, much less likely to duplicate in unrelated projects
    • but still no path information, easy to duplicate in large projects

Good include guard names (for file "config.h"):

  • PATE_20091116_142045
    • that's <last name>_<date>_<time>
    • no project, path, filename information even needed
    • easy to type
      • if your editor has an insert-date feature, you can "type" it very fast
    • easy to generate
      • include a sequence number when generating, if you need to generate more than one per second
    • strong guarantee of being universally unique
  • INCLUDE_GUARD_726F6B522BAA40A0B7F73C380AD37E6B
    • generated from an actual UUID
      • strong guarantee of being universally unique
    • if it turns up unexpectedly, "INCLUDE_GUARD" is a good hint about what it is, while serving to put it in a separate namespace (though by convention rather than recognized by the language)
    • prepend a project name, if desired (which is often required by project guidelines for macros)
    • easy to write your own sample program to generate
烦人精 2024-08-19 04:41:06

如果为已定义的常量添加#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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文