我正在使用数字食谱库编写代码,我想最小化一个实际上是类方法的函数。我有这种类型的代码:
class cl{
Doub instance(VecDoub_I &x)
{
return x[0]*x[0] + x[1]*x[1];
};
};
而我想在下面的代码中使用 Powell 方法最小化此函数
// enter code here
int main(void)
{
cl test;
Powell<Doub (VecDoub_I &)> powell(test.instance);
}
但是当我编译时出现以下错误:
main.cpp:241:22: error: invalid use of member function (did you forget the ‘()’ ?)
main.cpp:242:54: error: no matching function for call to ‘Powell<double(const NRvector<double>&)>::Powell(<unresolved overloaded function type>)’
有没有人遇到过这个问题? 提前致谢
最佳答案
由于cl::instance
是一个实例 方法(即不是静态方法),它需要一个this
指针。因此,您无法在 regular function pointer 中捕获指向它的指针。 .此外,为了获取函数的地址,您应该使用 &
运算符。
我不熟悉您正在使用的库,但我认为将函数更改为 static
(或使其成为自由函数)并添加 &
应该有帮助。
Powell<Doub (VecDoub_I &)> powell(&cl::instance);
关于c++ - 类方法类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29205888/