gpt4 book ai didi

objective-c - 将 void 传递给不兼容类型的参数?

转载 作者:太空宇宙 更新时间:2023-11-04 06:22:48 24 4
gpt4 key购买 nike

我试图在我的 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);

但是我得到以下错误:

Passing 'void' to parameter of incompatible type 'void (*)(double *, double *, double *)'

也许是 sleep 不足,但我有点困惑,因为我是 C 的新手。

最佳答案

您试图传递调用 _fmincg_evaluate 函数的结果,而不是传递实际的 _fmincg_evaluate 函数。

你想要:

fmincg(_fmincg_evaluate, inputFeatures->matrix, inputFeatures.elementCount, 50);

fmincg 的实现将负责调用传入的函数指针及其所需的任何参数。

更新:正如 Adrian 在下面的评论中指出的,您不能将 _fmincg_evaluate 函数作为第一个参数传递给 fmincg 函数,因为参数类型不'匹配。

关于objective-c - 将 void 传递给不兼容类型的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31526913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com