[英]warning C4715: 'operator==' : not all control paths return a value
I keep getting this error and I do not understand why. 我不断收到此错误,但我不明白为什么。 The compiler told me that it was in this section. 编译器告诉我它在本节中。
Any help would be greatly appreciated 任何帮助将不胜感激
bool operator==(const Bitmap& b1, const Bitmap& b2){
// TODO: complete the == operator
if ((b1.height == b2.height) && (b1.width == b2.width))
{
for (int r = 0; r < b1.height; r++)
{
for (int c = 0; c < b1.width; c++)
{
if (b1.get(r, c) == b2.get(r, c))
{
}
else
return false;
}
}
}
else
return false;
}
The diagnostic from the compiler is exactly what it says. 编译器的诊断正是它所说的。
Note that if the for loop runs through to the end, without taking the if condition that returns false
, if r
reaches the b1.height
value, the execution path will reach the end of this function without an explicit return
. 请注意,如果for循环一直运行到最后,而没有采用返回false
的if条件,则如果r
达到b1.height
值,则执行路径将到达该函数的末尾而没有显式的return
。
The error message is pretty clear. 错误消息很清楚。
bool operator==(const Bitmap& b1, const Bitmap& b2){
if ((b1.height == b2.height) && (b1.width == b2.width))
{
for (int r = 0; r < b1.height; r++)
{
for (int c = 0; c < b1.width; c++)
{
...
}
}
return ???; // What should be returned here?
}
else
return false;
}
The error message tells what is wrong. 错误消息告诉您出了什么问题。
bool operator==(const Bitmap& b1, const Bitmap& b2){
// TODO: complete the == operator
if ((b1.height == b2.height) && (b1.width == b2.width))
{
for (int r = 0; r < b1.height; r++)
{
for (int c = 0; c < b1.width; c++)
{
if (b1.get(r, c) == b2.get(r, c))
{
}
else
return false;
}
}
return true; // I guess you forgot this line
}
else
return false;
}
Well, it is precisely what the error says. 好吧,这正是错误所言。 The compiler doesn't know if there is a possibility that the code in the nested for loop can ever be triggered. 编译器不知道是否有可能触发嵌套的for循环中的代码。 Assuming the first condition is true, there is a chance that the code never gets to the return statement. 假设第一个条件为真,那么代码就永远不会到达return语句。 Therefore the compiler will make sure that something always is returned, regardless of whatever condition you give it. 因此,无论您提供何种条件,编译器都将确保始终返回某些内容。
bool operator==(const Bitmap& b1, const Bitmap& b2){
if ((b1.height == b2.height) && (b1.width == b2.width))
{
// The compiler expects a return statement that is always reachable inside this if.
for (int r = 0; r < b1.height; r++)
{
for (int c = 0; c < b1.width; c++)
{
if (b1.get(r, c) == b2.get(r, c))
{
}
else
return false; //This isn't always reachable.
}
}
}
else
return false; // This case is covered, so nothing wrong here.
}
It's simple. 这很简单。
bool operator==(const Bitmap& b1, const Bitmap& b2){
// TODO: complete the == operator
if ((b1.height == b2.height) && (b1.width == b2.width))
{
for (int r = 0; r < b1.height; r++)
{
for (int c = 0; c < b1.width; c++)
{
if (b1.get(r, c) == b2.get(r, c))
{
}
else
return false;
}
}
// if your function runs to end of for loop, you get to here
}
else
return false;
//then here
return false; //<-- add this
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.