控制在bool函数中到达非void函数的结束

控制在bool函数中到达非void函数的结束

本文介绍了控制在bool函数中到达非void函数的结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前正在工作的位的代码,我似乎无法找出一种方法使此功能停止抛出控制到达结束非void函数错误。

  bool operator ==( const MyInt& x,const MyInt& y)
{
if(x.currentSize!= y.currentSize)
{
return false;
}
else
{
for(int i = 0; i {
if(x.integer [ i]!= y.integer [i])
{
return false;
break;
}
else
return true;
}
}
}


解决方案>

您必须在第一个 else 上返回,以防您的方法不通过for循环,例如 x.currentSize = 0



此外,第一次迭代,所以你应该这样改变,所以你可以检查 Array

  bool operator ==(const MyInt& x,const MyInt& y)
{
if(x.currentSize!= y.currentSize)
{
return false;
}
else
{
for(int i = 0; i if(x.integer [i]!= y .integer [i])
return false;
return true;
}
}


This is the bit of code that I'm currently working on and I can't seem to figure out a way to make this function stop throwing the "control reaches end of non-void function" error. Shouldn't the else statement catch anything the if statement doesn't and return either true or false?

bool operator == (const MyInt& x, const MyInt& y)
{
    if (x.currentSize != y.currentSize)
    {
        return false;
    }
    else
    {
        for (int i = 0; i < x.currentSize; i++)
        {
                if (x.integer[i] != y.integer[i])
                {
                    return false;
                    break;
                }
                else
                    return true;
        }
    }
}
解决方案

You must put a return on the first else, in case your method doesn't go through the for loop, for example, when x.currentSize = 0

Also, your if condition always returns a value after the first iteration, so you should change it like this, so you can check all elements in the Array

bool operator == (const MyInt& x, const MyInt& y)
{
    if (x.currentSize != y.currentSize)
    {
        return false;
    }
    else
    {
        for (int i = 0; i < x.currentSize; i++)
                if (x.integer[i] != y.integer[i])
                    return false;
        return true;
    }
}

这篇关于控制在bool函数中到达非void函数的结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:12