#include #include #include #include namespace Test{ struct f_params{ double a; double b; }; //Struct definition, //properly namespaced inside Test. class Testo { private: gsl_integration_workspace * w; gsl_function * pIntegrand; double (*pTheFunction)(double, void*); public: Testo(): w( gsl_integration_workspace_alloc(1000)), pIntegrand( new gsl_function() ) {}; //No need for extra semicolon in empty constructor body. Testo( double (*pNewFunction)(double, void*)) : //Construct a //Testo object //with a pointer //to the function //to be integrated. w( gsl_integration_workspace_alloc(1000)), pIntegrand( new gsl_function() ), pTheFunction(pNewFunction) {}; ~Testo() { gsl_integration_workspace_free(w); delete pIntegrand; }; double Integrate() { double resIntegral, abserr; f_params list= {0.0,1.0}; //C-style struct initialisation ok, //but I prefer: // // f_params list; // list.a = 0.0; // list.b = 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 = pTheFunction; pIntegrand->params = &list; gsl_integration_qag (pIntegrand, x_start, x_end, epsabs, epsrel, limit, 1, w, &resIntegral, &abserr); return resIntegral; }; }; double f (double x, void * params) { f_params * pList = (f_params *) params; double a = (pList->a); double b = (pList->b); return a+b*x; } }//end namespace Test int main(void) { //If you prefer to avoid the scope resolution operators below, //(i.e. the :: operator)uncomment the following two using //declarations. Note that this opens up the std and Test namespaces //only inside the current function, main(), instead of dumping all //of std and Test into the global namespace, which would defeat the //purpose of namespaces to begin with. //using namespace Test //using namespace std; Test::Testo G(&Test::f); //Create object of type Testo and assign it //Test::f as the function it will //integrate. std::cout << G.Integrate() << std::endl; return 0; }