The exact warning I get is
warning C4715: \'hand::show\' : not all control paths return a value
and hand::show is
std::ostr
You can do what sth said, or, since in this case you're really returning the same thing either way...
std::ostream& hand::show(std::ostream& os) const
{
if(side == left)
{
os<<display[0]<<display[1]<<display[2]<<display[3]<<display[4];
}
else
{
os<<display[4]<<display[3]<<display[2]<<display[1]<<display[0];
}
return os;
}
The error means that if side is neither left nor right, your function will not return a value - either side is declared improperly or your enum is. An enum should be defined like
enum orientation {left, right};
So try changing your orientation structure to that.
Your compiler isn't smart enough to take into account that the only two options for side are left and right, so it thinks it's possible for neither return statement to be executed. When side is neither left nor right, your function doesn't say which value to return.
If side is not left or right, then the return value is undefined.
Even though orientation is an enum with only two values (right now), it can still have a different value for any of the following reasons:
side might be uninitialized, so it could be neither left nor rightside might have been assigned another value via typecasting, e.g. *((int*)&side) = 2 Possible solutions include:
if with an else as suggested by sth.Change it to be:
if(side == left) {
return ...;
} else if(side == right) {
return ...;
} else {
...handle error...
}
The warning means it's possible to go through your method without returning any explicit value. With your code:
std::ostream& hand::show(std::ostream& os) const
{
if(side == left)
{
return os<<display[0]<<display[1]<<display[2]<<display[3]<<display[4];
}
if(side == right)
{
return os<<display[4]<<display[3]<<display[2]<<display[1]<<display[0];
}
}
if side != left and side != right, then you don't return anything. A common way of fixing this problem is to assume, for example, if not "left" then always assume "right":
std::ostream& hand::show(std::ostream& os) const
{
if(side == left)
{
return os<<display[0]<<display[1]<<display[2]<<display[3]<<display[4];
}
return os<<display[4]<<display[3]<<display[2]<<display[1]<<display[0];
}
To get rid of the warning, replace the second if with an else:
std::ostream& hand::show(std::ostream& os) const
{
if(side == left)
{
return os<<display[0]<<display[1]<<display[2]<<display[3]<<display[4];
}
else
{
return os<<display[4]<<display[3]<<display[2]<<display[1]<<display[0];
}
}