Problem with an enum in a switch statement

A

army1987

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.)
 
A

army1987

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.
 
V

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top