Problem with an enum in a switch statement

Discussion in 'C++' started by army1987, Sep 5, 2012.

  1. army1987

    army1987 Guest

    I have two types `eParticleType` and `Particle` defined as
    enum eParticleType {
    eNull,
    eNucleus,
    eMuon,
    eElectron,
    eProton,
    eNeutron,
    ePhoton,
    eNeutrino,
    ePion
    };
    class Particle {
    public:
    // lots of stuff
    private:
    eParticleType fType;
    // more stuff
    };
    and a function defined as
    std::vector<Particle> PropagateParticle(Particle* input) {

    std::vector<Particle> output;
    // stuff

    switch(fType) {

    case eNeutron:

    case eProton:

    case eNucleus:

    output = PropagateNucleus(input);

    break;

    case eNeutrino:

    output = PropagateNeutrino(input);

    break;

    // etc.
    default:

    std::cerr << "Unsupported particle type" << std::endl;

    abort();

    }
    // stuff

    return output;

    }
    (Each of the `PropagateXxx` functions has `assert(input->GetType() ==
    eXxx;` at the beginning.)


    When I compile this with g++, I get "warning: case label value exceeds
    maximum value for type", and when I run the program I immediately get
    "Assertion `input->GetType() == eMuon' failed.". What's going on? (And
    no, I'd rather not have stuff like `class Nucleus: public Particle` etc.
    for backward compatibility reasons.)



    --
    [ T H I S S P A C E I S F O R R E N T ]
    Troppo poca cultura ci rende ignoranti, troppa ci rende folli.
    -- fathermckenzie di it.cultura.linguistica.italiano
    <http://xkcd.com/397/>
     
    army1987, Sep 5, 2012
    #1
    1. Advertising

  2. army1987

    army1987 Guest

    On Wed, 05 Sep 2012 19:38:31 +0000, army1987 wrote:
    [snip]
    > When I compile this with g++, I get "warning: case label value exceeds
    > maximum value for type", and when I run the program I immediately get
    > "Assertion `input->GetType() == eMuon' failed.". What's going on? (And
    > no, I'd rather not have stuff like `class Nucleus: public Particle` etc.
    > for backward compatibility reasons.)


    Never mind, I've just realized that the function from which I was trying
    to use the private member `fType` wasn't itself a member of `class
    Particle`, so I was using the fType of another class.



    --
    [ T H I S S P A C E I S F O R R E N T ]
    Troppo poca cultura ci rende ignoranti, troppa ci rende folli.
    -- fathermckenzie di it.cultura.linguistica.italiano
    <http://xkcd.com/397/>
     
    army1987, Sep 5, 2012
    #2
    1. Advertising

  3. On 9/5/2012 3:38 PM, army1987 wrote:
    > I have two types `eParticleType` and `Particle` defined as
    > enum eParticleType {
    > eNull,
    > eNucleus,
    > eMuon,
    > eElectron,
    > eProton,
    > eNeutron,
    > ePhoton,
    > eNeutrino,
    > ePion
    > };
    > class Particle {
    > public:
    > // lots of stuff
    > private:
    > eParticleType fType;
    > // more stuff
    > };
    > and a function defined as
    > std::vector<Particle> PropagateParticle(Particle* input) {


    Is that a member function? Because if it is, then it needs to be
    defined slightly differently, probably:

    std::vector<Particle> Particle::propagateParticle( ...

    And if it isn't, then what's "fType" in it?

    >
    > std::vector<Particle> output;
    > // stuff
    >
    > switch(fType) {
    >
    > case eNeutron:
    >
    > case eProton:
    >
    > case eNucleus:
    >
    > output = PropagateNucleus(input);
    >
    > break;
    >
    > case eNeutrino:
    >
    > output = PropagateNeutrino(input);
    >
    > break;
    >
    > // etc.
    > default:
    >
    > std::cerr << "Unsupported particle type" << std::endl;
    >
    > abort();
    >
    > }
    > // stuff
    >
    > return output;
    >
    > }
    > (Each of the `PropagateXxx` functions has `assert(input->GetType() ==
    > eXxx;` at the beginning.)
    >
    >
    > When I compile this with g++, I get "warning: case label value exceeds
    > maximum value for type", and when I run the program I immediately get
    > "Assertion `input->GetType() == eMuon' failed.". What's going on? (And
    > no, I'd rather not have stuff like `class Nucleus: public Particle` etc.
    > for backward compatibility reasons.)


    See FAQ 5.8 and follow its recommendations.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 5, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gane kol

    switch case with enum

    gane kol, May 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,310
    Kevin Spencer
    May 6, 2005
  2. Bruce Sam
    Replies:
    2
    Views:
    4,147
    Andrew McDonagh
    Jan 10, 2005
  3. Replies:
    21
    Views:
    1,060
    Giannis Papadopoulos
    Aug 2, 2005
  4. bthumber
    Replies:
    5
    Views:
    421
    Alexey Smirnov
    Jan 29, 2009
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    110
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page