0

I am trying switch case and inheritance in C++ and found some problems/ warnings.

For example I have an abstract basic class Field:

Field.h class Field{ private: FieldType type_; public: enum FieldType { GRASS,WATER,STREET,HOME,TOWNHALL }; virtual bool checkIsBuildable(Fieldtype type); 

Now I get warnings in subclasses Buildings.cpp and Properties.cpp:

 warning enumeration value GRASS,WATER,STREET bit handled in switch 

Since it is a bool i just can return false or true in default and the method wont work propely or? I just want to check for example Home and Townhall in Buildings.cpp and Grass, Water and street in Properties.

Buildings.cpp bool Buildings::isBuildable(Field::FieldType type) { switch(type) { case Field::HOME: return true; case Field::TOWNHALL: return false; } } Properties.cpp bool Properties::isBuildable(Field::FieldType type) { switch(type) { case Field::GRASS: return true; case Field::WATER: return false; case Field::STREET: return false; } } 
5
  • 8
    You need to handle all the cases. What if the type isn't any of the one you list in the switch? What do you return then? Add a default case. Commented May 2, 2018 at 11:27
  • Apart from the missing default: you don't need to repeat identical code in every case, you can just do: case X: caseY: commonXYCode(); break; Commented May 2, 2018 at 11:29
  • The warning is that your switch doesn't cover all the values in your enum. The warning is suggesting to add a default case so it is clear that all other cases not explicitly mentioned are handled. Commented May 2, 2018 at 11:29
  • And if you logically won't have a default case, because the caller already should have arranged for that, then a throw std::invalid_argument makes sense for the default case. Commented May 2, 2018 at 11:29
  • For a decision this simple I wouldn't use a switch statement. return type == HOME || type == TOWNHALL;. Commented May 2, 2018 at 11:50

1 Answer 1

5

You need to add default: return true; or return false; in this context;

bool Properties::isBuildable(Field::FieldType type) { switch(type) { case Field::GRASS: return true; case Field::WATER: return false; case Field::STREET: return false; default: return false; } } 

Or just add return out of switch scope:

bool Properties::isBuildable(Field::FieldType type) { switch(type) { case Field::GRASS: return true; case Field::WATER: return false; case Field::STREET: return false; } return false; } 

Because if your type won't be equal to one of the values from case then function won't return any value, you need to fix it with a help of methods shown above.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh true, since the virtual methode will just called themself in the right class. Thanks alot !
you could then delete a collection of lines so that you have case Field::Grass: case Field::Concrete: return true; default: return false;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.