为什么编译器会在此代码的最后warning: Expression results unused
语句中发出else
?
PowerballLottery::WinningPossibility PowerballLottery::checkTicket(PowerballTicket ticket)
{
int count = 0;
if (ticket.getBall1() == getball1() || ticket.getBall1() == getball2() || ticket.getBall1() == getball3() || ticket.getBall1() == getball4() || ticket.getBall1() == getball5())
count++;
if (ticket.getBall2() == getball1() || ticket.getBall2() == getball2() || ticket.getBall2() == getball3() || ticket.getBall2() == getball4() || ticket.getBall2() == getball5())
count++;
if (ticket.getBall3() == getball1() || ticket.getBall3() == getball2() || ticket.getBall3() == getball3() || ticket.getBall3() == getball4() || ticket.getBall3() == getball5())
count++;
if (ticket.getBall4() == getball1() || ticket.getBall4() == getball2() || ticket.getBall4() == getball3() || ticket.getBall4() == getball4() || ticket.getBall4() == getball5())
count++;
if (ticket.getBall5() == getball1() || ticket.getBall5() == getball2() || ticket.getBall5() == getball3() || ticket.getBall5() == getball4() || ticket.getBall5() == getball5())
count++;
bool match = false;
if (ticket.getPowerball() == getpowerball())
match = true;
if ((count == 0) && (match == false))
return PowerballLottery::WinningPossibility::NOTWINNING;
else if ((count == 0) && (match == true))
return PowerballLottery::WinningPossibility::POWERBALL;
else if ((count == 1) && (match == true))
return PowerballLottery::WinningPossibility::ONEPLUSPOWERBALL;
else if ((count == 2) && (match == true))
return PowerballLottery::WinningPossibility::TWOPLUSPOWERBALL;
else if ((count == 3) && (match == false))
return PowerballLottery::WinningPossibility::THREE;
else if ((count == 3) && (match == true))
return PowerballLottery::WinningPossibility::THREEPLUSPOWERBALL;
else if ((count == 4) && (match == false))
return PowerballLottery::WinningPossibility::FOUR;
else if ((count == 4) && (match == true))
return PowerballLottery::WinningPossibility::FOURPLUSPOWERBALL;
else if ((count == 5) && (match == false))
return PowerballLottery::WinningPossibility::FIVE;
else ((count == 5) && (match == true));
return PowerballLottery::WinningPossibility::FIVEPLUSPOWERBALL;
}
答案 0 :(得分:3)
实际上,代码是
if ((count == 0) && (match == false))
....
else if ((count == 5) && (match == false))
return PowerballLottery::WinningPossibility::FIVE;
else
((count == 5) && (match == true)); //<- Expression results unused
return PowerballLottery::WinningPossibility::FIVEPLUSPOWERBALL;
答案 1 :(得分:3)
其他人已经指出了编译器警告你的问题。
现在让我们考虑一下它没有警告你的问题:你的代码要比它有任何好的理由复杂得多。
从本质上讲,用户选择和在抽奖中选择的球(除了力量球)形式集。我们关心的是这两组的交叉点的大小。 C ++标准库为我们提供了一些工具,使这个任务变得更加简单(而且我认为你设计PowerBallTicket
类的方式比起帮助更多地阻碍了):
std::set<int> lottery {
getball1(),
getball2(),
getball3(),
getball4(),
getball5()
};
std::set<int> tick {
ticket.getball1(),
ticket.getball2(),
ticket.getball3(),
ticket.getball4(),
ticket.getball5()
};
std::vector<int> matches;
std::set_intersection(lottery.begin(), lottery.end(),
tick.begin(), tick.end(),
std::back_inserter(matches));
这给了我们两者之间的匹配。从那里,我们有转换匹配数量(加上Powerball是否匹配)的问题,以选择返回哪个枚举值。一种简单的方法是使用数组:
WinningPossibility rets[][6] = {
{ NOTWINNING, ONE, TWO, THREE, FOUR, FIVE},
{ POWERBALL, ONEPLUS, TWOPLUS, THREEPLUS, FOURPLUS, FIVEPLUS }
};
有了这些,我们的回报看起来像这样:
return rets[ticket.getpowerball() == getpowerball()][matches.size()];
上面的代码指向了一个改进:代替所有那些getballX()
成员函数,您应该只将数据存储在set
中,然后直接在这些集合上运行。在这种情况下,看起来你有quasi-classes,其中“封装”失去了很多,并且获得了很少(可能没什么)。
答案 2 :(得分:1)
正如@AlexD&#39; answer所述,这主要是您如何格式化代码的问题,并且出现了警告,因为
else ((count == 5) && (match == true));
该语句实际上并不用于存储在结果中的任何计算,或基于它做出分支决策。
return PowerballLottery::WinningPossibility::FIVEPLUSPOWERBALL;
语句无论如何都会被执行。
提醒一下着名的apple OpenSSL goto fail;
bug:
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) goto fail; goto fail; /* MISTAKE! THIS LINE SHOULD NOT BE HERE */
答案 3 :(得分:0)
您可以说else <statement>
或else if (<expression>) <statement>
;你有没有if
的表达式,编译器正在尽力使用它。