我试图在我的Objective-C项目中使用C函数,但我无法将它们混合使用。
我有以下功能:
// 1. Pass in the cost function which takes in an array and gives out cost and gradient at the given input.
// 2. xVector should contain the initial point which is will be modified to reflect the optimum point
// 3. nDim is the dimension of xVector
// 4. maxCostCalls is the maximum number of times the cost function may be called
// return value: 1 -> Num of Cost function calls exceeded max specified in the argument. 2-> line search failed
int fmincg(void (*costFunc)(double* inputVector, double* cost, double* gradVector), double* xVector, int nDim, int maxCostFuncCalls); xVector, int nDim, int maxCostFuncCalls);
这是我传递给第一个参数的函数:
static void _fmincg_evaluate(id thisClass, LKDataset *inputFeatures, LKFeature *outputFeature, LKMatrixObject *initialWeights, double cost, LKMatrixObject *optimizedWeights) {
cost = [thisClass costUsingLogisticRegressionWithInputFeatures:inputFeatures outputFeature:outputFeature andWeights:initialWeights];
optimizedWeights = [thisClass optimizeWeightsUsingGradientDescentForInputFeatures:inputFeatures outputFeature:outputFeature andWeights:initialWeights];
}
最后要调用fmincg,我会执行以下操作:
fmincg(_fmincg_evaluate(self, inputFeatures, outputFeature, self.weights, cost, optimizedWeights), inputFeatures->matrix, inputFeatures.elementCount, 50);
但是我收到以下错误:
传递'无效'到不兼容类型的参数' void(*)(double *,double *,double *)'
也许是因为睡眠不足,但我有点困惑,因为我是C的新手。
答案 0 :(得分:4)
您正在尝试传递调用The documentation for this class was generated from the following file:
*source/file1/doxyTest.cs
函数的结果,而不是传递实际的_fmincg_evaluate
函数。
你想:
_fmincg_evaluate
fmincg(_fmincg_evaluate, inputFeatures->matrix, inputFeatures.elementCount, 50);
的实现将负责调用传入的函数指针及其所需的任何参数。
更新:正如Adrian在下面的评论中指出的那样,您不能将fmincg
函数作为第一个参数传递给_fmincg_evaluate
函数,因为参数类型不适用匹配。