我有一个C++代码,我真的需要使用C函数在它:函数指针和C++模板
int procedure(... , S_fp fun , ...)
其中的乐趣 - 是一个函数指针,其签字必须是这样的:
int fun(double* , double* , double)
功能,指针,我想用的,是一个类模板的成员:
template<int nPar> class PenaltyAlgorithm
{
public:
...
int Calculate(double* param, double* val, double prec)
{
...
}
...
}
正如你可以看到,它的签名是有序的。但是当我这样做时: 程序(... & PenaltyAlgorithm :: Calculate,...);
我得到错误:
error: cannot convert ‘int (PenaltyAlgorithm<30>::*)(double*, double*, double)’ to ‘int (*)(...)’ for argument ...
请帮助。如果您需要任何其他信息,请写下。
谢谢!
阅读的答案后,我理解,这增加有关类的信息是很重要的:
template<int nPar> class PenaltyAlgorithm
{
public:
int Calculate(double* param, double* val, double prec)
{
*val = comp_fun->Compute(param);
}
double* RunAlgorithm()
{
...
procedure(... &PenaltyAlgorithm<nPar>::Calculate, ...);
...
}
...
private:
...
CompositeFunction<nPar>* comp_fun;
}
1)。我不能使用静态乐趣,因为这个乐趣需要获得对类的成员的访问; 2)。我们可以使用这样一个事实,即我们从有趣的类的成员中调用'过程'吗?
尝试'静态INT演算....' –
上调用类的函数传递一个隐含的这个指针,以便您的签名显然是错误的。你应该阅读[C++ FAQ](http://www.parashift.com/c++-faq-lite/pointers-to-members.html) – stonemetal