【问题标题】:Warning about flow control in the function关于函数中的流量控制的警告
【发布时间】:2017-02-03 03:19:24
【问题描述】:

我做了一个函数来检查数组是否有负数,然后返回值;它将int testArray[] 作为第一个参数,int n=14 作为数组大小。我使用了一个 for 循环来遍历数组。我正在使用if 语句来比较testArray[i]<0,并且我有一个else 语句来打印一条消息,指出没有找到负数。代码编译没有错误,但我没有输出。我收到警告:

In function 'int countNegative(int*, int)': 28:1: warning: control reaches end of non-void function [-Wreturn-type]

我怀疑参数传递给函数的方式可能存在问题。

    #include <iostream>
    #include <cstdlib>
    using namespace std;

    int countNegative(int testArray[],int n);

    int main(){
        int testArray[] = {-2,0,44,12,-45,17,934,-21,67,88,91,1,0,6};
        int n = 14;

        countNegative(testArray,n);

        system("PAUSE");
        //EXIT_SUCCESS;
        return 0;
    }

    int countNegative(int testArray[],int n){
        for(int i=0; i<n; i++){
            if(testArray[i]<0){
                int index = testArray[i];
                return index;
            }
            else{
                cout << "No Negative Numbers";
            }
        }
    }

【问题讨论】:

    标签: c++ arrays function for-loop control-flow


    【解决方案1】:

    您的 countNegative 函数存在多个问题。

    int countNegative(int testArray[],int n){
        for(int i=0; i<n; i++){
            if(testArray[i]<0){
                int index = testArray[i]; // <= You are returning value here, not the index in the array.
                return index;
            }
            else{
                cout << "No Negative Numbers";
                // No return here, should have returned 0 ?
            }
        }
        // No return here ?
    }
    

    从函数名看,好像是要统计testArray中的负值,返回负值的总数。

    为什么会收到此警告?

    这是因为,假设testArray 中没有负数。在这种情况下,您不会返回任何内容,即您的控件也可以在没有任何返回值的情况下到达您的 else 语句。控件也可能到达函数的末尾而不从那里返回任何值。由于您已将返回类型标记为 int,因此在所有这些条件下都必须返回一个整数值。

    如果我的理解是正确的,您应该重构您的函数以仅迭代数组并计算负条目的总数。最后,您可以返回该值。

        int countNegative(int testArray[],int n){
            int total_negatives = 0;
            for(int i=0; i<n; i++){
                if(testArray[i]<0){
                    total_negatives++;
                }
            }
            if (total_negatives == 0)  cout << "No Negative numbers\n";
            return total_negatives;
        }
    

    complete-program

    【讨论】:

      【解决方案2】:

      你应该有一个 int 变量来接收你的函数调用的返回值。您编写的代码将仅返回在您的数组中找到的第一个负数的第一个索引。如果你想要一个负数的计数,那么你不应该马上返回。此外,如果您的数组最终没有负值,那么您永远不会返回任何东西,您只会打印出没有负值的消息,并且您最终会以写入方式为数组中的每个项目打印该消息.

      我会像这样重写它。此函数将返回在数组中找到的负数的计数,如果没有找到负数,则返回 0。

      int countNegative(int testArray[],int n){
          int negs = 0;
      
          for(int i=0; i<n; i++){
              if(testArray[i]<0){
                  negs++;
              }
          }
      
          return (negs);
      
      }
      

      然后你应该像这样改变你的主要功能。

      int main(){
          int testArray[] = {-2,0,44,12,-45,17,934,-21,67,88,91,1,0,6};
          int n = 14;
          int foundNegatives = countNegative(testArray,n);
          if ( ! foundNegatives ) {
               cout << "No Negative Numbers";
          }
      
          system("PAUSE");
          //EXIT_SUCCESS;
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 2018-06-29
        • 2013-11-03
        • 1970-01-01
        • 2015-09-29
        • 2010-09-25
        • 2022-10-15
        相关资源
        最近更新 更多