从“int(*)(int)”到“int”的无效转换

有人可以向我解释为什么我会return somatorio; 在第 24 行收到此警告吗?

从“int (*) (int)”到“int”的无效转换

#include <iostream>

using namespace std;

int somatorio(int soma){
    soma+=soma;
    return soma;
}

int fatorial (int n){
    if (n==0)
        return 1;
    else 
        return (n * fatorial (n-1));
}
        
int combinacao(int num){
    int k = 1;
    while(k<=num){
    int combinacao = (fatorial(num)) / ((fatorial(k)) * (fatorial(num - k)));
    somatorio (combinacao);
    k++;
    }
    return somatorio;  // <-- Warning here
}

int main()
{
    int num;
    cin>>num;
    cout<<combinacao(num)<<endl;
    return 0;
}

回答

int combinacao(int num),你有return somatorio;

根据函数签名,您必须返回一个整数。但是somatorio是一个接受 anint并返回一个int( int (*)(int))的函数,它本身不是一个int

我假设您要计算二项式系数的总和是否正确?在这种情况下,我可以告诉您,您提供的汇总系数的方法不会按预期工作。实际上,您只将每个值乘以 2,而从不保存结果。

如果要对值求和,则必须定义一个变量来存储总和,将系数添加到该变量中,然后返回存储在该变量中的值。

  • @VictoriaMoraes That depends on what you expect the code to do, which you haven't really explained.
  • @VictoriaMoraes There are lots of strange things in the code. I expect the warning is not the only thing that is wrong with your code. But until you say what it is supposed to do it's hard for anyone to make any suggestions.
  • Just edited my answer. I hope it is clearer now what you have to do.

以上是从“int(*)(int)”到“int”的无效转换的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>