【问题标题】:C, "conflicting types for... " errorC、“冲突类型...”错误
【发布时间】:2014-03-10 05:24:34
【问题描述】:

在我继续之前,这是给我一个错误的代码:

#define numScores 3             // the number of test scores which a student will have

struct btreenode{
int studentID;              // the ID number of the student at the current node

float scores[3];            // the 3 test scores of the student

float average;              // the average of the 3 test scores for the student

struct btreenode *left;     // pointer to left side of the tree
struct btreenode *right;    // pointer to right side of the tree
};

typedef struct btreenode *Node;

编译时出现以下错误:

btreenode.h:17: error: redefinition of 'struct btreenode'
btreenode.h:28: error: conflicting types for 'Node'
btreenode.h:28: note: previous declaration of 'Node' was here

我在顶部有一个块注释,所以行号是关闭的,但是

第 17 行是第一行“struct btreenode{

第 28 行是最后一行“typedef struct btreenode *Node

有人知道我为什么会收到这些错误吗?

【问题讨论】:

  • 你的意思是struct btreenode *Node;
  • 对我来说没有错误。 See online demo of successful compilation.
  • 也为我编译成功。
  • @jlzizmor 如果您在源文件.c 文件中也声明了相同的结构或在源文件中包含两次头文件,则可能会出现此错误。您可以显示源文件和头文件。
  • 看起来您不止一次地包含了具有这些定义的文件。使用标题保护。

标签: c struct compiler-errors typedef redefinition


【解决方案1】:

头文件不应包含多次。所以在头文件中使用宏来避免多重包含。

#ifndef TEST_H__
#define TEST_H__

/*you header file can have declarations here*/

#endif /* TEST_H__*/

我认为,这种方法在你的头文件中是没有的。

【讨论】:

    【解决方案2】:

    看起来你的 btreenode.h 文件被多次包含(直接或间接)......这就是为什么“先前声明”和“冲突类型”在同一文件中的同一行(先前声明在第一个包含中,当它在下一个包含中遇到同一行时,类型冲突)。

    您应该使用头文件保护(在 btreenode.h 中)来防止头文件代码在已包含的情况下被处理。在文件顶部添加:

    #ifndef BTREENODE_H
    #define BTREENODE_H
    

    并在文件末尾添加:

    #endif // BTREENODE_H
    

    这样,只有当 BTREENODE_H 不是先前包含的 #defined 时,才会编译它们之间的任何内容。

    【讨论】:

      猜你喜欢
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-07
      • 2013-03-25
      • 2023-03-31
      • 2012-10-04
      相关资源
      最近更新 更多