c++ - 类方法类型

标签 c++ numerical-recipes

我正在使用数字食谱库编写代码,我想最小化一个实际上是类方法的函数。我有这种类型的代码:

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/

相关文章:

c++ - 为什么 Numerical Recipes 头文件中没有 include 守卫?

c++ - 将包含 std::thread 的类添加到 vector 中

c++ - 集群节点的 libmemcached 故障转移

c++ - 对大数进行因式分解

c++ - 确定范围内的元素放置在连续内存中

c++ - 将构造函数值从子类传递到父类(super class) C++

c - 关于《数值食谱》一书的问题,第 2 版。 : allocation/deallocation of memory for vectors