2014-02-12 70 views
3

我繼續之前,這裏是它給了我一個錯誤代碼:C,「衝突的類型......」錯誤

#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

有誰知道我爲什麼會收到這些錯誤?

+0

你的意思是'struct btreenode * Node;'? – herohuyongtao

+0

對我來說沒有錯誤。 [查看成功編譯的在線演示](http://codepad.org/r2pHTtuE)。 –

+0

也爲我編譯成功。 –

回答

6

頭文件不應該包含多次。所以在頭文件中使用宏以避免多重包含。

#ifndef TEST_H__ 
#define TEST_H__ 

/*you header file can have declarations here*/ 

#endif /* TEST_H__*/ 

我認爲,這種方法不在你的頭文件中。

1

它看起來好像您的btreenode.h文件被多次包含(直接或間接)......這就是爲什麼「以前的聲明」和「衝突的類型」在同一行(同一行第一個包含聲明時,在下一個包含同一行時包含衝突類型)。

如果已經包含頭文件代碼,則應該使用頭文件防護(在btreenode.h中)來防止處理頭文件代碼。在文件的頂部,添加:

#ifndef BTREENODE_H 
#define BTREENODE_H 

,並在文件末尾添加:

#endif // BTREENODE_H 

這樣一來,無論是那些之間,如果BTREENODE_H是不是已經#define d只會被編譯從以前的包含。