【问题标题】:Function is not returning any value | C++函数没有返回任何值 | C++
【发布时间】:2020-05-07 21:44:19
【问题描述】:

我正在编写一个函数,它将找到具有最大除数的数字,但该函数不返回任何内容。有人可以指出我的错误吗?

这是问题

编写一个 C++ 程序,创建一个包含 30 个元素的整数数组。在这个数组中获取输入(在 main 功能)。之后,使用引用指针将该数组传递给名为“Find_Max_Divisors”的函数。 函数“Find_Max_Divisors”应该在数组中找到(并返回)具有最高的数字 除数的数量。最后,主函数显示除数最多的数字。

#include <iostream>
using namespace std;

int main ()
{
    int arr[30];
    int* array = &arr[30];
    cout << "Please enter values of the array" << endl;
    for (int i=0; i<30; i++)
    {
        cin >> arr[i];
    }
    cout << "Number with most divisors in array is " << endl;
    int Find_Max_Divisors (*array);
}

int Find_Max_Divisors (int p[])
{
    int count=0, max_divisor, max_counter, prev=0, repeat=0, divisor;
    for (int i=2; i<=30; i++)
        {
            if (p[i]%i==0)
            {
                count++;
            }
            if (count > prev)
            {
                prev = count;
                divisor = p[i];
            }
            if (count==max_counter && max_counter!=0)
            {
                cout << p[i] <<" has maximum of "<< count <<" divisors.\n";
            }
        max_counter = prev;
        max_divisor = divisor;
        repeat++;
        }
        return count;
}

【问题讨论】:

  • 你说函数没有返回任何东西,但你怎么知道函数被调用了?
  • @drescherjm 这对我来说看起来像是一个常规的旧变量声明,它应该编译得很好:)
  • C++ 肯定不好解析
  • i&lt;=30 可能是 1 错误。
  • 我们不会在问题标题中添加“SOLVED”。您表示通过接受答案解决了问题。 (我修正了标题。)

标签: c++


【解决方案1】:

改变

int Find_Max_Divisors (*array);

int value = Find_Max_Divisors(arr);

您可以完全摆脱 array 变量。

你很可能会发现你也需要把你的函数放在main之前。

【讨论】:

  • 仅此更改并不能真正解决问题。
  • 记住int* array = &amp;arr[30];
  • 哈哈。我错过了。想知道 OP 是如何在没有来自编译器的有用警告的情况下走到今天的。
  • 许多新人忽略了警告。
【解决方案2】:

首先,您声明一个包含 30 个元素的数组

int arr[30];

但是在这里你让指针指向arr的外面。

int* array = &arr[30];

我猜你想让指针指向arr,如果我没记错的话,你可以这样做:

int *array = &arr[0]; // or int * array = arr;

那么当你调用Find_Max_Divisors函数时,你应该改为:

int return_value = Find_Max_Divisors(array);

还有一件事,int这个函数:

for (int i=2; i<=30; i++)

i=30p[i] 再次跳出弹框。应该是:

for (int i=2; i< 30; i++)

【讨论】:

  • 我们也可以利用array decayint *array = arr;
  • 是的,在这种情况下,我只想强调他的观点。我的意思是指针应该指向第一个元素而不是元素 31。我编辑了。安,谢谢你的评论。
【解决方案3】:

你不需要指针来做这个简单的代码可以解决你的问题只需改变你想要的数组的大小我在这里用大小为 4 的数组进行测试

#include <iostream>
using namespace std;

int Find_Max_Divisors(int p[])
{
    int count = 0, max = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 1; j < p[i] / 2; j++) {
            if (p[i] % j == 0) {
                count++;
            }
        }
        if (count > max)
            max = p[i];
    }
    return max;
}
int main()
{
    int arr[30];
    // int* array = &arr[30];
    cout << "Please enter values of the array" << endl;
    for (int i = 0; i < 4; i++) {
        cin >> arr[i];
    }
    int value = Find_Max_Divisors(arr);
    cout << "Number with most divisors in array is " << value << endl;
}

【讨论】:

    【解决方案4】:

    你的代码有几个错误:

    • 首先,如果您的main 函数应该知道它调用的函数,您应该事先声明它们。只需在main函数前添加一行Find_Max_Divisors (int p[]);即可。

    • C 或 C++ 中的数组指针,当您仅通过其名称调用它时。所以调用Find_Max_Divisors (arr) 并摆脱那个糟糕的指针分配。

    • 在最后一行尝试调用函数,但不要把它放到stdout,你应该把它改成这样:

    cout << "Number with most divisors in array is " << Find_Max_Divisors(arr) << endl;
    

    您实际上对 int Find_Max_Divisors (*array); 所做的是声明一个新变量而不是调用函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      相关资源
      最近更新 更多