2009-04-30 62 views
0

這可能看起來像一個簡單的問題,但編譯時出現錯誤。我希望能夠枚舉傳遞到一個方法C.在C中傳入枚舉

枚舉

enum TYPES { PHOTON, NEUTRINO, QUARK, PROTON, ELECTRON }; 

調用方法

makeParticle(PHOTON, 0.3f, 0.09f, location, colour); 

方法

struct Particle makeParticle(enum TYPES type, float radius, float speed, struct Vector3 location, struct Vector3 colour) 
{ 
    struct Particle p; 
    p.type = type; 
    p.radius = radius; 
    p.speed = speed; 
    p.location = location; 
    p.colour = colour; 

    return p; 
} 

我正的錯誤是我打電話時的方法:

不兼容的類型分配

+0

大多數人稱之爲「功能」而不是「方法」,但我們都知道你在說什麼。 – 2009-04-30 22:48:12

回答

5

它編譯爲我好,在這個刪節例如:

enum TYPES { PHOTON, NEUTRINO, QUARK, PROTON, ELECTRON }; 

void makeParticle(enum TYPES type) 
{ 
} 

int main(void) 
{ 
    makeParticle(PHOTON); 
} 

你確定你所做的TYPES提供的聲明代碼在makeParticle的定義和它的調用?它不會工作,如果你這樣做:

int main(void) 
{ 
    makeParticle(PHOTON); 
} 

enum TYPES { PHOTON, NEUTRINO, QUARK, PROTON, ELECTRON }; 

void makeParticle(enum TYPES type) 
{ 
} 

因爲main()代碼還沒有見過類型呢。

-2

嘗試改變

p.type = type; 

p.type = (int)type; 

如果這沒有幫助,請加全.c文件,其中包括struct Particle定義你的問題。