【问题标题】:Receiving "Control reaches end of non-void function" error message in Xcode在 Xcode 中接收“控制到达非无效函数的末尾”错误消息
【发布时间】:2011-10-02 11:34:24
【问题描述】:

将此代码添加到一个简单的计算器程序时,我收到错误消息“控制到达非空函数的结尾”。

现在,在阅读了一些回复后,我知道我应该添加一个返回调用,这样程序就不会在未设置累加器的情况下挂起,但我想知道将它放在哪里或者哪个是正确的表达方式。

谢谢,任何帮助将不胜感激。如果需要,我也可以提供完整的程序代码。

-(double) changeSign
{
accumulator = -accumulator;
}

-(double) reciprocal
{
accumulator = 1/accumulator;

}

-(double) xSquared
{
accumulator = accumulator * accumulator;
}

【问题讨论】:

    标签: objective-c xcode calculator


    【解决方案1】:

    如果你定义为

    -(void)methodName{
    //No need to return any thing.
    }
    

    如果你写了任何返回类型。

    -(int)methodName{
    return int
    }
    

    示例

    -(void)getData{
    //Write any thing without return.
    }
    
    -(int)getData{
    int total = a + b;
    return total;
    }
    

    或者

    -(int)getData{
    return a + b;}
    

    【讨论】:

      【解决方案2】:

      问题是,当我们使用“void”以外的返回类型时,我们应该向调用函数返回一些内容 例如:

      (returnType)methodName
      {
            ---required code---
            return something(as;-1/0/1/particular Value);
      }
      

      【讨论】:

        【解决方案3】:

        您的代码表明这些方法都将返回一个double

        - (double) reciprocal
        { // ^^ Return type
        ...
        

        但它们都不包含return 语句。您需要添加这些值才能将值传递回调用者:

        - (double) reciprocal
        {
            accumulator = 1 / accumulator;
            return accumulator;
        }
        

        然后在调用代码中你可以把这个方法的结果赋值给一个变量:

        double foo = [someObject reciprocal];
        

        或者,如果您不打算将值传回(也许您只是在更新一些内部状态),请将返回类型更改为 void

        - (void) reciprocal
        { // ^^ Indicates that there is no return value
        ...
        

        【讨论】:

        • 谢谢,这是我在看到答案之前曾经纠正过的,但正如你所说,-(void) 会是更好的选择。
        【解决方案4】:

        非常简单,您在此处拥有的每个方法都被声明为返回 double,但您实际上从未返回任何内容。

        您需要执行以下操作:

        -(double) changeSign
        {
        accumulator = -accumulator;
        return accumulator;
        }
        

        或者,或者,如果您不打算返回 accumulator 的新值,请将返回类型更改为 void

        -(void) changeSign
        {
        accumulator = -accumulator;
        }
        

        ps。问题不在于程序将“在未设置累加器的情况下挂起”。就是您说“这里有一个返回double 的方法”,但代码实际上并没有返回任何内容。错误消息的意思是“我到了你承诺会返回一些东西的这个函数的末尾,但你似乎忘记了”。

        【讨论】:

        • 谢谢,我打算将其设置为(void),但示例中说将其用作(double),我觉得这不是完全必要的,但还是试了一下。也感谢您清理错误消息背后的原因
        【解决方案5】:

        您需要returndouble。方法的基本语法是

        -(return type)methodName
        

        如果accumulator 是已声明的属性,那么您的函数不会返回任何内容,即void,就像这样

        -(void) changeSign
        {
        accumulator = -accumulator;
        }
        

        因此,[self changeSign] 之类的调用不会返回任何内容,但会更改 accumlator 的值。

        或者,您可以有一个返回类型并返回accumulator by

        -(double) changeSign
        {
        return -accumulator;
        }
        

        然后你可以这样做:

        self.accumulator = [self changeSign];
        

        我怀疑前者是你所追求的,但希望它能让语法更清晰。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-04-22
          • 2016-05-07
          • 2014-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多