我想在C++中應用牛頓方法,現在只是測試如果我的指針工作和正確。現在的問題是它不能調用函數來測試這個,它說轉換有問題。函數指針作爲參數的C++問題
我的代碼:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
double newton(double (*f) (double), double (*fPrime)(double), double intialValue, int iterations);
double f(double x);
double fPrime(double x);
int main() {
int limitIterations = 0;
double intialValue = 0;
cout << "Please enter a starting value for F(X): " ;
cin >> intialValue;
cout << endl << "Please enter the limit of iterations performed: " ;
cin >> limitIterations;
cout << newton(intialValue, limitIterations);
return 0;
}
double f(double x) {
double y;
y =(x*x*x)+(x*x)+(x);
return (y);
}
double fPrime(double x){
double y;
y = 3*(x*x) + 2 * x + 1;
return (y);
}
double newton(double (*f) (double), double (*fPrime)(double), double intialValue, int iterations){
double approxValue = 0;
approxValue = f(intialValue);
return (approxValue);
}
和錯誤:
|26|error: cannot convert 'double' to 'double (*)(double)' for argument '1' to 'double newton(double (*)(double), double (*)(double), double, int)'|
謝謝!這是我需要的修復!讓我的代碼工作,現在找到零哈哈 – ChonBonStudios