C++で関数やその他の関数にパラメータを固定して渡す方法があるかどうかを知りたいと思います。yを固定したパラメータとして関数f(x、y)を渡します。
ここexemple説明するために:
#include<math.h>
#include<iostream>
double myfunc(double x) { return 5*x; }
double gaussian(double x , double s , double v) { return exp((x-v)*(x-v)/(s*s)) ; }
double Trapeze1D(const double x_min, const double x_max, double (*fonction)(double))
{
double nb_pts = 100.0 ;
double integrale = 0.0 ;
double dx = (x_max - x_min)/(double) nb_pts ;
double x_k ;
while (k < nb_pts-1)
{
k++;
x_k = x_min + (double) k * dx ;
integrale += fonction(x_k) ;
}
integrale *= dx ;
return integrale ;
}
int main()
{
cout << Trapeze1D(0.0,1.0,myfunc) << endl ; // <-- works fine
cout << Trapeze1D(0.0,1.0, gaussian(double , 2,3)) << endl ; // <-- I would like something like that ...
// Also tried :
double tmp(double x) = gaussian(x, 2,3) ; // <-- does not work C++ does not allow definition of function inside function ...
cout << Trapeze1D(0.0,1.0, tmp) << endl ;
}
を私の問題は以下の通りです、私は(積分法は、最終的にははるかに洗練されただろう)の機能のいくつかの異なるタイプを統合し、それらの多くする必要があります
class gaussian
{
public:
double eval(double x) ;
void get(double s, double v) ;
private:
double s ;
double v ;
}
double gaussian::eval(double x){ return exp((x-v)*(x-v)/(s*s)) ; }
int main()
{
gaussian G;
G.get(1.0,2.0) ;
cout << Trapeze1D(0.0,1.0,G.eval) << endl ; // <-- error
}
が、私は目を取得する:私も、このようなクラスを使用してみましたがガウス、多項式、ベータ法律...
などのパラメータに依存します次のエラー:非静的メンバー関数への参照を呼び出す必要があります
ご協力いただければ幸いです。 多くのありがとう
は、サウンドと'std :: bind'。 – NathanOliver
@NathanOliver彼はいません – Sopel