C、 "conflicting types for... "错误

标签 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

我在顶部有一个 block 注释,因此行号已关闭,但是

第 17 行是第一行“struct btreenode{

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

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

最佳答案

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

#ifndef TEST_H__
#define TEST_H__

/*you header file can have declarations here*/

#endif /* TEST_H__*/

我假设,您的头文件中没有这种方法。

关于C、 "conflicting types for... "错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21718562/

相关文章:

c - 常量数组的全局变量的替代品?

c++ - C中的异构函数指针数组

c - 与仅使用结构体相比,使用结构体指针有什么好处?

c - 制作我自己的 strlen 函数,以接收字符串作为链接列表

java - 使用代码获取黄色错误/警告

Haskell 使用 foldr 计算长度抛出类型错误

typescript - TypeScript-为什么在编译时不知道设置为已定义对象的静态属性要定义?

c - 我需要将许多函数中使用的全局变量设置为本地变量

用openssl实现ECDSA签名和验证的C实现

objective-c - 结构内存分配(泄漏和崩溃,iPhone 上的分析)