2014-11-16 43 views
0

我有兩個.h文件和.c文件如下向前聲明拋出重新定義錯誤

typedef struct mode_info_t_ mode_info_t; 

struct common { 
    int create; 
    mode_info_t *mode_info; 
}; 

BH

typedef struct mode_info_t_ { 
    int primary; 
    int secondary; 
} mode_info_t; 

的main.c

#include "a.h" 
#include "b.h" 

----- 

當編譯.c時,它拋出錯誤 -

b.h:17:錯誤的:typedef 'mode_info_t' 的重新定義
A.H:50:錯誤: 'mode_info_t' 以前的聲明在這裏

這裏有什麼問題的專家?

+0

我不是用typedef專家,但你作爲一個結構typedefing mode_info_t兩次,一次是另一個變量,並再次...還能說什麼? – Sam

+0

正是錯誤所說的......你已經重新定義了一個類型。這不是'結構mode_info_t_'雖然,但'mode_info_t'你Typedef的吧。 – Dmitri

+0

@BLUEPIXY你說得對。還有一件事是爲什麼在同一個結構中多次執行'typedef'。並且還在結構中使用用戶定義的數據類型? – Gopi

回答

0

變化b.h這樣:

struct mode_info_t_ { 
    int primary; 
    int secondary; 
}; 

如果您需要在b.h typedef的,有b.h包括A.H.如果你不希望有b.h包括A.H但仍需要在這兩個類型定義,然後採取的typedef出來A.H,並把它放在c.h,並同時擁有A.H和b.h包括C.H.

我,我把我所有的前置聲明在一個單獨的頭前,其實這樣做,只是爲了避免需要各種頭,包括對方的時候它不是完全合適的。

+0

是的,我將b.h改爲typedef struct mode_info_t_ { int primary; int secondary; } mode_info; – Jithu

0

你有你的BH

typedef struct mode_info_t_ { 
     int primary; 
      int secondary; 
}mode_info_t; 

然後啊

struct common { 
     int create; 
     mode_info_t *mode_info; 
}; 

在你main.c中只包含BH

#include "b.h" 
    #include "a.h" 


    int main() 
    { 
    } 
+0

我不能做這條路。因爲a。h被包含在許多其他c文件中,我不想公開b.h. – Jithu

+0

@Jithu然後按編輯版本 – Gopi

0

當我編譯你給的代碼片段,我沒沒有任何錯誤和警告。你能告訴我們你的完整代碼嗎?可以聲明一個結構,然後你可以定義它。

typedef struct Node Node; 
struct Node { 
    int data; 
    Node *nextptr; 
}; 

在這裏你正在做同樣的事情。所以這不是一個錯誤。你可能在某處錯誤地處理了它。

+0

顯示的方式重新發布。 – Jithu