Frage

Das mag wie eine einfache Frage erscheinen, aber ich bin immer einen Fehler, wenn dies zu kompilieren. Ich möchte in der Lage, eine ENUM in ein Verfahren in C zu übergeben.

Enum

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

Der Aufruf der Methode

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

Methode

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

Der Fehler erhalte ich, wenn ich die Methode nenne:

  

inkompatible Typen in Zuordnung

War es hilfreich?

Lösung

Es kompiliert gut für mich, in diesem abgespeckte Beispiel:

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

void makeParticle(enum TYPES type)
{
}

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

Sind Sie sicher, dass Sie die Erklärung von TYPES auf den Code zur Verfügung gestellt haben sowohl in der Definition von makeParticle und dem Aufruf es? Es wird nicht funktionieren, wenn Sie dies tun:

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

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

void makeParticle(enum TYPES type)
{
}

, weil der main() Code noch nicht TYPEN gesehen.

Andere Tipps

Versuchen Sie,

p.type = type;

p.type = (int)type;

Wenn das nicht hilft, fügen Sie bitte die gesamte C-Datei, einschließlich der Definition von struct Particle auf Ihre Frage.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top