在 C++ 中调用 C 函数
call c function in c++
我尝试创建一个包装类来包装库中的 c 函数lmdif1
cminpack
class CSolver
{
public:
void solve()
{
...
using namespace std::placeholders;
auto f = std::bind(&CSolver::fcn, this, _1, _2, _3, _4, _5, _6);
int32_t iRet = lmdif1(f, 0, m_iEqualCount, m_iUnknownVariableCount, x, fvec, tol, iwa, wa, lwa);
}
private:
int32_t fcn(void* p, int32_t m, int32_t n, const double* x, double* fvec,int iFlag)
{
....
}
};
编译错误:
error: cannot convert ‘std::_Bind<std::_Mem_fn<int (CSolver::*)(void*, int, int, const double*, double*, int)>(CSolver*, std::_Placeholder<1>, std::_Placeholder<2>, std::_Placeholder<3>, std::_Placeholder<4>, std::_Placeholder<5>, std::_Placeholder<6>)>’ to ‘cminpack_func_mn {aka int (*)(void*, int, int, const double*, double*, int)}’ for argument ‘1’ to ‘int lmdif1(cminpack_func_mn, void*, int, int, double*, double*, double, int*, double*, int)’
int32_t iRet = lmdif1(f, 0, m_iEqualCount, m_iUnknownVariableCount, x, fvec, tol, iwa, wa, lwa);
我该如何解决?
编辑:所以我将使用全局函数。谢谢大家。
不能获取成员函数的函数指针。它必须是一个类函数。类函数在 C++ 中使用 static 关键字声明。因此,您所要做的就是将fcn的原型更改为:
static int32_t fcn (void *p, int32_t m, int32_t n, const double *x,
double *fvec, int iFlag)
当然,您将无法从 fcn 中访问任何非静态成员变量。
希望这有帮助!
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销