-1
#include <stdio.h>

double soma ( double a, double b){
    return a+b;
}

double mult ( double a, double b){
    return a*b;
}

double sub( double a, double b){
    return a-b;
}

double div ( double a, double b){
    return a/b;
}

double fib_ninja ( double (* fn)(double a, double b),int init ){
    int i=0;
    int tam=10;
    int acum = init;
    int ant=0;

    for (i=0 ; i<tam ; i++){
        acum = fn(acum,ant);
        ant = acum;
        printf ("%f",acum);
    }
    return acum;
}

int main(){
    int op;
    printf("escolha a operação desejada: 1(soma),2(multiplicação),3(subtraçaõ,4(divisão)) ");
    scanf("%d",&op);        
    if(op==1){ 
        fib_ninja((soma (6.0, 2.0)),0);
    }
    if(op==2){
        fib_ninja((mult ,6.0, 2.0),1);
    }
    if(op==3) {
        fib_ninja((sub ,6.0, 2.0),0);
    }
    if(op==4){
        fib_ninja((div ,6.0, 2.0),1);
    }

    return 0;
}

错误说

In function 'main':
Line 39: error: incompatible type for argument 1 of 'fib_ninja'
Line 42: error: incompatible type for argument 1 of 'fib_ninja'
Line 45: error: incompatible type for argument 1 of 'fib_ninja'
Line 48: error: incompatible type for argument 1 of 'fib_ninja'

来自此http://codepad.org/HTLeR6Jh的链接

4

2 回答 2

3

我试图推断你在这里想要做什么,所以如果我误解了,请告诉我。

第一:acum并且需要antfib_ninja()double

第二:我不知道您要对值6.02.0. 它们没有在签名中声明或在 中的任何地方使用fib_ninja(),并且没有必要将它们传递给您的soma()等人。函数,因为fib_ninja()它显然意味着采用函数指针,而不是double从执行这些函数中返回。从调用中删除参数6.02.0(以及多余的括号),fib_ninja这将消除错误。

例如:
fib_ninja(soma, 0);
fib_ninja(mult, 1);

修复这些东西后,您的代码仍然不会做太多事情。如果您在进行更多工作后还有其他问题,请发布另一个问题。

于 2013-09-13T17:00:31.310 回答
0

该程序有点混乱,如果不进行一些更改,可能不会做任何有用的事情。我建议发布您要回答的问题。

尽管如此,编译器的直接问题还是相当简单的。

double Add(double x, double y);

int main()
{
    // The following will execute Add() and
    // pass the result to func1() as a double.
    func1(Add(1.0, 2.0));

    // The following will pass a function pointer
    // for Add() to func2()
    func2(Add);

    return 0;
}

我想你想要func2()上面的东西,但你写的东西更像func1().

于 2013-09-13T18:40:28.307 回答