C++ 警告C4715:&x27;运算符==';:并非所有控制路径都返回值

C++ 警告C4715:&x27;运算符==';:并非所有控制路径都返回值,c++,C++,我一直在犯这个错误,我不明白为什么。编译器告诉我它在这一部分 任何帮助都将不胜感激 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.

我一直在犯这个错误,我不明白为什么。编译器告诉我它在这一部分

任何帮助都将不胜感激

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;
}
bool运算符==(常量位图和b1、常量位图和b2){
//TODO:完成==运算符
如果((b1.height==b2.height)和&(b1.width==b2.width))
{
对于(int r=0;r
编译器的诊断正是它所说的


请注意,如果for循环一直运行到末尾,而没有采用返回
false
的if条件,如果
r
达到
b1.height
值,则执行路径将到达此函数的末尾,而没有显式的
返回

,错误消息非常清楚

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;
}
bool运算符==(常量位图和b1、常量位图和b2){
如果((b1.height==b2.height)和&(b1.width==b2.width))
{
对于(int r=0;r
错误消息告诉您出了什么问题

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;
}
bool运算符==(常量位图和b1、常量位图和b2){
//TODO:完成==运算符
如果((b1.height==b2.height)和&(b1.width==b2.width))
{
对于(int r=0;r
嗯,这正是错误所说的。编译器不知道嵌套for循环中的代码是否有可能被触发。假设第一个条件为true,则代码可能永远不会到达return语句。因此,编译器将确保无论您给出什么条件,都会返回某些内容

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.
}
bool运算符==(常量位图和b1、常量位图和b2){
如果((b1.height==b2.height)和&(b1.width==b2.width))
{
//编译器需要一个返回语句,该语句在该if中始终是可访问的。
对于(int r=0;r
这很简单

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
}
bool运算符==(常量位图和b1、常量位图和b2){
//TODO:完成==运算符
如果((b1.height==b2.height)和&(b1.width==b2.width))
{
对于(int r=0;rreturn false;//想象一下,如果b1.height、b2.height、b1.width和b2.width都为零。在这种情况下,您将输入第一个代码块,但永远不会输入两个for循环中的任何一个……并且您也不会在最后碰到“return false”,因为这只是针对“else”的相反,您只需删除函数的底部,函数返回的值将是未定义的。