gpt4 book ai didi

C++: "error collect2: error: ld returned 1 exit status"

转载 作者:行者123 更新时间:2023-11-28 04:58:13 25 4
gpt4 key购买 nike

我正在编写一个简单的程序来计算函数的导数,但我总是得到错误:

collect2: error: ld returned 1 exit status

这是我的程序:

#include <iostream>
#include <stdlib.h>
#include <math.h>

using namespace std;

double derivative2(double (fun), double step, double x);
double fun(double);

int main(int argc, char* argv[]) {
double h = atof(argv[1]);
double x = sqrt(2);
cout << derivative2(fun(x), h, x) << endl;
return 0;
}


double derivative2(double fun(double), double step, double x) {
return ((fun(x + step) - fun(x))/step);
}


double fun(double x) {
return atan(x);
}

我找到了 this post ,但它对我没有用。

最佳答案

double derivative2(double (fun), double step, double x);

double derivative2(double fun(double), double step, double x)

是不同的东西。在第一个声明中 fundouble,在第二个 fun 中是 double(*)(double) (指向函数的指针)。

因为这个函数是在一点求导,所以右边的声明就是函数指针那个。

修复:

double derivative2(double fun(double), double step, double x); // 'fun' is a function pointer.

// ...

cout << derivative2(fun, h, x) << endl; // Pass fun as a function pointer.

关于C++: "error collect2: error: ld returned 1 exit status",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46709774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com