我在使用一个非常复杂的C函数时遇到了问题,这个函数是在一个C++类中使用的(重写C函数不是一种选择)。C函数:
typedef void (*integrand) (unsigned ndim, const double* x, void* fdata,
unsigned fdim, double* fval);
// This one:
int adapt_integrate(unsigned fdim, integrand f, void* fdata,
unsigned dim, const double* xmin, const double* xmax,
unsigned maxEval, double reqAbsError, double reqRelError,
double* val, double* err);
我需要提供一个返回类型为integrand
的空函数,然后adapt_integrate
将计算n维积分。如果func
是一个独立的函数,下面的calcTripleIntegral
代码可以作为独立的函数工作。
我想传递一个(非静态!)类成员函数作为被积函数,因为这样可以轻松地进行重载等操作...
class myIntegrator
{
public:
double calcTripleIntegral( double x, double Q2, std::tr1::function<integrand> &func ) const
{
//...declare val, err, xMin, xMax and input(x,Q2) ...//
adapt_integrate( 1, func, input,
3, xMin, xMax,
0, 0, 1e-4,
&val, &err);
return val;
}
double integrandF2( unsigned ndim, const double *x, void *, // no matter what's inside
unsigned fdim, double *fval) const; // this qualifies as an integrand if it were not a class member
double getValue( double x, double Q2 ) const
{
std::tr1::function<integrand> func(std::tr1::bind(&myIntegrator::integrandF2, *this);
return calcTripleIntegral(x,Q2,func);
}
}
在GCC 4.4.5(预发布版)上,这给了我以下错误:
error: variable 'std::tr1::function func' has initializer but incomplete type
编辑:我的代码有什么错误?我现在已经尝试使用GCC 4.4、4.5和4.6编译,结果都是相同的错误。要么没有对此进行任何处理,要么是我做错了/编辑
非常感谢!如果我表达不清楚,我很乐意解释。
附注:我能否通过在myIntegrator.cpp中使用指向某个函数的函数指针来解决这个问题而不使用tr1的东西?
最终更新:好吧,我错误地认为TR1提供了一个一两行的解决方案。很遗憾。我正在将我的类转换为命名空间并复制粘贴函数声明。我只需要一个基类和一个重新实现接口的子类。C函数指针+C++类=对我来说不好的消息。 无论如何,感谢所有的答案,你们向我展示了C++的一些黑暗角落;)
fdata
和input
参数是关于什么的?为什么积分类型中的void*
参数没有名称? - deft_codevoid* fdata
是否被传递 - 如果integrand
的void*
参数命名得当,我们就不必猜测/询问了。 - Georg Fritzscheintegrand
中的fdata参数,即涉及数学函数的用户提供的参数。 - rubenvb