簡體   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