Question

Cela peut sembler une question simple, mais je reçois une erreur lors de la compilation. Je veux pouvoir passer une énumération dans une méthode en C.

Enum

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

Appel de la méthode

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

Méthode

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

L'erreur que je reçois est lorsque j'appelle la méthode:

  

types incompatibles dans l'affectation

Était-ce utile?

La solution

Cela compile bien pour moi, dans cet exemple réduit:

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

void makeParticle(enum TYPES type)
{
}

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

Êtes-vous sûr d'avoir rendu la déclaration de TYPES disponible pour le code à la fois dans la définition de makeParticle et dans l'appel de ce code? Cela ne fonctionnera pas si vous faites ceci:

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

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

void makeParticle(enum TYPES type)
{
}

car le code main () n'a pas encore vu TYPES.

Autres conseils

Essayez de changer

p.type = type;

à

p.type = (int)type;

Si cela ne résout pas le problème, ajoutez tout le fichier .c, y compris la définition de struct Particle à votre question.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top