TF1 and user defined function of a class

Hi,
I have a class. In this I have a member function which I want to use as a user defined function, giving it as the second parameter of the TF1 construtor:
TF1(const char* name, Double_t ()(Double_t, Double_t*) fcn, Double_t xmin = 0, Double_t xmax = 1, Int_t npar = 0)

So, e.g.:

class myclass{
private:
TF1 * myF1;
(…)
public:
myclass();
double myfunc(double * x, double * p);
void set_myF1();
(…)
};

So the source would be:

double myclass::myfunc(double * x, double * p){
//something
return value;
}

void myclass::set_myF1(){
myF1= new TF1(“myfunc”, “myclass::myfunc”);
}

But I get this error message:

ResFit.cxx: In member function void ResFit::set_cfit_function()': ResFit.cxx:190: invalid conversion fromconst void*’ to void*' ResFit.cxx:190: initializing argument 2 ofTF1::TF1(const char*, void*,
double, double, int)’

I am using Root 4.04/02b with gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

Balint

Hi,

you can’t pass a member function’s address. Define myfunc as static, or make it (or a wrapper of it) a global function.

Axel.

Hi,

Okay, thanks for the tip, but why isn’t this feature implemented? Why cannot one give a member function as a user defined function?

Balint

[quote=“radbalint”]Hi,
I have a class. In this I have a member function which I want to use as a user defined function, giving it as the second parameter of the TF1 construtor:
TF1(const char* name, Double_t ()(Double_t, Double_t*) fcn, Double_t xmin = 0, Double_t xmax = 1, Int_t npar = 0)

So, e.g.:

class myclass{
private:
TF1 * myF1;
public:
myclass();
double myfunc(double * x, double * p);
void set_myF1();
};

So the source would be:

void myclass::set_myF1(){
myF1= new TF1(“myfunc”, “myclass::myfunc”);
}

Balint[/quote]

In C++ pointer to non-static member-function and pointer to function are completely different things. So, you cannot convert

void (T::)(SomeType) into void ()(SomeType).
^ ^
pointer to member-function pointer to function

But you can use static member-function (of course, it may be bad for you, because static function cannot accsess non-static data without object expression).

[quote=“radbalint”]Hi,

Okay, thanks for the tip, but why isn’t this feature implemented? Why cannot one give a member function as a user defined function?

Balint[/quote]

Pointer to member function cannot be used without an object, it always
require an object

class A
{
public:
void fun(some_type){}
};

void g(A * ptr, A & obj)
{
void (A::*pfd)(some_type) = &A::fun;
(ptr->*pfn)(some_args);
(obj.*pfn)(some_args);
}