Question

This may seem like a simple question but i am getting an error when compiling this. I want to be able to pass an enum into a method in C.

Enum

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

Calling the method

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

Method

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;
}

The error I am getting is when I am calling the method:

incompatible types in assignment

Was it helpful?

Solution

It compiles fine for me, in this cut-down example:

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

void makeParticle(enum TYPES type)
{
}

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

Are you sure that you've made the declaration of TYPES available to the code in both the definition of makeParticle and the call to it? It won't work if you do this:

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

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

void makeParticle(enum TYPES type)
{
}

because the main() code hasn't seen TYPES yet.

OTHER TIPS

Try changing

p.type = type;

to

p.type = (int)type;

If this doesn't help, please add the whole .c file, including the definition of struct Particle to your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top