[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-gsl] Integration with C++
From: |
Ivan Liu |
Subject: |
[Help-gsl] Integration with C++ |
Date: |
Sun, 29 Oct 2006 17:29:45 +0100 |
Hi,
I would like to use the numerical integration routine in a C++ class
environment, and I wrote the following test routine
#include <iostream>
#include <gsl/gsl_integration.h>
using namespace std;
/************************/
// Integrand
/* #(1)
struct f_params{ double a; double b; };
double f (double x, void * params) {
struct f_params * pList = (struct f_params *) params;
double a = (pList->a);
double b = (pList->b);
return a+b*x;
};
*/
/***************************/
class Testo
{
private:
gsl_integration_workspace * w;
gsl_function * pIntegrand;
public:
Testo():
w( gsl_integration_workspace_alloc(1000)),
pIntegrand( new gsl_function() )
{ ;};
/********************/
/* #(2) */
struct f_params{ double a; double b; };
double f (double x, void * params) {
struct f_params * pList = (struct f_params *) params;
double a = (pList->a);
double b = (pList->b);
return a+b*x;
};
/********************/
double Integrate()
{
double resIntegral, abserr;
struct f_params list= {0.0,1.0};
double x_start=0.0;
double x_end=1.0;
int limit=999;
double epsabs=0.0;
double epsrel=1e-6;
pIntegrand->function = &f;
pIntegrand->params = &list;
gsl_integration_qag (pIntegrand, x_start, x_end, epsabs, epsrel,
limit, 1,
w, &resIntegral, &abserr);
return resIntegral;
};
};
int main(void)
{
Testo G;
cout << G.Integrate() << endl;
return 0;
}
The program doesn't compile and the message is:
test.cpp: In member function 'double Testo::Integrate()':
test.cpp:62: error: ISO C++ forbids taking the address of an unqualified or
parenthesized non-static member function to form a pointer to member
function. Say '&Testo::f'
test.cpp:62: error: cannot convert 'double (Testo::*)(double, void*)' to
'double (*)(double, void*)' in assignment
If I comment out the section marked #(2) and use #(1) instead, the program
works no problem,
but I'd like to put the integrand into the class.
How do I do that?
thanks,
Ivan Liu
- [Help-gsl] Integration with C++,
Ivan Liu <=