Domanda

Questa può sembrare una semplice domanda ma sto ricevendo un errore durante la compilazione. Voglio essere in grado di passare un enum in un metodo in C.

Enum

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

Chiamare il metodo

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

Metodo

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'errore che sto ricevendo è quando chiamo il metodo:

  

tipi incompatibili nell'incarico

È stato utile?

Soluzione

Compila bene per me, in questo esempio ridotto:

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

void makeParticle(enum TYPES type)
{
}

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

Sei sicuro di aver reso la dichiarazione di TYPES disponibile per il codice sia nella definizione di makeParticle che nella chiamata ad essa? Non funzionerà se lo fai:

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

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

void makeParticle(enum TYPES type)
{
}

perché il codice main () non ha ancora visto TYPES.

Altri suggerimenti

Prova a cambiare

p.type = type;

a

p.type = (int)type;

Se questo non aiuta, aggiungi l'intero file .c, inclusa la definizione di struct Particle alla tua domanda.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top