Reputation:
The exact warning I get is
warning C4715: 'hand::show' : not all control paths return a value
and hand::show is
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];
}
}
where side is a variable of type orientation
orientation{
left = -1,
right = 1
};
What does the warning mean, and what would be the best solution to get rid of it?
Upvotes: 3
Views: 57366
Reputation: 111876
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 right
side
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...
}
Upvotes: 6
Reputation: 156188
As others suggest, the problem is that your side
might be neither left
nor right
.
You may modify your function to do any of the following:
Upvotes: 1
Reputation:
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;
}
Upvotes: 3
Reputation: 229643
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];
}
}
Upvotes: 2
Reputation: 1853
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.
Upvotes: 20
Reputation: 54421
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];
}
Upvotes: 4
Reputation: 16015
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.
Upvotes: 6