C++:"错误收集 2:错误:ld 返回 1 退出状态"

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

本文关键字:错误 退出 状态 返回 C++ ld      更新时间:2023-10-16

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

collect2:错误:ld 返回 1 个退出状态

这是我的程序:

#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);
}

找到了这篇文章,但它对我的情况没有用。

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

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

是不同的东西。在第一个声明中fundouble,在第二个fundouble(*)(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.
相关文章: