【问题标题】:“redefinition of 'foo' as different kind of symbol”“将‘foo’重新定义为不同的符号”
【发布时间】:2011-03-04 08:48:56
【问题描述】:

我正在尝试重构和引入一些旧代码,我遇到了这样的事情:

          struct foo;
typedef   struct foo *    foo;

在尝试编译时,我收到以下错误:

Source/Types/Types.h:27:18: error: redefinition of 'foo' as different kind of symbol
typedef   struct foo *    foo;
                 ^

有人知道这是什么原因吗?我已经很久没有接触过代码了,但我当然不记得任何与此相关的错误。它是代码库中一些最核心的代码,一切都取决于它;我不明白我怎么可能错过这样一个明显的错误,如果它确实是一个错误的话。由于原来的foo 是一个“结构标记”(在struct 关键字之后只是一个合理的引用),它怎么会与我的新foo typedef 类型冲突?

编辑 1: 这是整个实际文件,也许我遗漏了一些东西,但看起来很简单。它会转储 slew 的上述相同错误,每种类型都有一个:

# if !defined(TYPE_DECLARATIONS)
#   define    TYPE_DECLARATIONS

# include "Core.h"
# warning "in Core.h"

static int class = 0; // to prove I’m not compiling as C++

          struct e(fork);
typedef   struct e(fork)*                 e(fork);

          struct e(execution);
typedef   struct e(execution)*            e(execution);


          struct e(thing);
typedef   struct e(thing)                 e(thing);

          struct e(typeRepresentation);
typedef   struct e(typeRepresentation)*   e(typeRepresentation);


struct e(typeRepresentation) {
  void *                      family;
  char                        name[64]; };

struct e(thing) {
  void * const                pointer;
e(typeRepresentation) const   isa; };


# endif //!defined(TYPE_DECLARATIONS)

(另外,忽略 e() 宏;在这种情况下它是一个 noop。)

【问题讨论】:

  • 该代码编译对我来说没有问题。你用的是什么编译器?我在 gcc 4.3.3
  • 看来您确实在尝试编译为C++。尝试在合适的地方添加int class; 并检查编译器是否抱怨:)
  • static int class = 0; 在全局范围内就在其他代码导致没有新错误之前

标签: c types typedef identifier redefinition


【解决方案1】:

啊,我解决了我自己的问题。我设法搞砸了我的 e() 宏,以至于它实际上是在执行一个 noop,删除有问题的代码。 :O

我是个白痴。很抱歉浪费了大家的时间。

【讨论】:

    【解决方案2】:

    您现在是否将其编译为 C++,而它曾经被编译为 C?

    在 C++ 中,struct 和 enum 标记与其他类型名称位于相同的命名空间中。

    【讨论】:

    • 这听起来像是完美的解释,但我不是。我希望这很容易)-:(我使用的是clang -std=c99 -pedantic-errors -Wall -O0 ...
    • @sbi,在 C++ 中没有这比这更复杂。 structunionenum 标记可以用作没有关键字 if 的类型,没有其他标识符(类型、函数、变量...)具有该名称。
    • @sbi:不完全。最初给出的代码不是正确的 C++,因为它生成的 typedefstruct foo 的语义不同。但是你的第二句话是错误的。例如 struct foo; foo* foo; 是有效的 C++,在 C 中无效。struct foo foo; struct foo* foo; 对两者都有效。 typedef struct foo foo; 也。但是typedef struct foo foo; foo* foo; 对两者都无效。
    • @Jens:我明白了。我没有考虑这些。现在定句正确吗?
    • @sbi、hm、类型和其他标识符存在于同一个命名空间中,在这里我们没有传递性......我会选择类似“在 C++ 中,结构和枚举标签可以如果没有歧义,则用作类型名称”或类似的东西。但无论如何,没有多少机会有人会研究这个现在已经过时的问题。
    【解决方案3】:

    您将名称 foo 与两种不同的类型相关联。 struct foo 和指向 struct foo 的指针是不同的类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多