繁体   English   中英

C、“冲突类型……”错误

[英]C, “conflicting types for… ” error

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

#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

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

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

#ifndef TEST_H__
#define TEST_H__

/*you header file can have declarations here*/

#endif /* TEST_H__*/

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

看起来好像你的 btreenode.h 文件被多次包含(直接或间接)......包含,当它在下一个包含中遇到同一行时发生冲突的类型)。

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

#ifndef BTREENODE_H
#define BTREENODE_H

并在文件末尾添加:

#endif // BTREENODE_H

这样,只有在 BTREENODE_H 尚未从先前的包含中#define d 时,才会编译它们之间的任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM