C,“......的冲突类型”错误

时间:2014-02-12 04:20:15

标签: c struct compiler-errors typedef redefinition

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

#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

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

2 个答案:

答案 0 :(得分:7)

头文件不应包含多次。因此,在头文件中使用宏来避免多次包含。

#ifndef TEST_H__
#define TEST_H__

/*you header file can have declarations here*/

#endif /* TEST_H__*/

我认为,这种方法不在您的头文件中。

答案 1 :(得分:1)

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

您应该使用标题保护(在btreenode.h中)来防止头文件代码被处理(如果已经包含)。在文件顶部,添加:

#ifndef BTREENODE_H
#define BTREENODE_H

并在文件末尾添加:

#endif // BTREENODE_H

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

相关问题