Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with an enum in a switch statement

Reply
Thread Tools

Problem with an enum in a switch statement

 
 
army1987
Guest
Posts: n/a
 
      09-05-2012
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/>
 
Reply With Quote
 
 
 
 
army1987
Guest
Posts: n/a
 
      09-05-2012
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/>
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-05-2012
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:ropagateParticle( ...

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My Switch statement get stuck on the last statement bthumber ASP .Net 5 01-29-2009 07:56 PM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
enum within an enum - Java 6 06-13-2005 12:51 AM
How to use enum that are not int type in the switch( ) statement in java? Bruce Sam Java 2 01-10-2005 08:20 AM



Advertisments