2009-04-30 68 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定义你的问题。