Objective c 是否将void传递给不兼容类型的参数?
我试图在我的Objective-C项目中使用C函数,但我很难将它们混合起来 我有以下功能:Objective c 是否将void传递给不兼容类型的参数?,objective-c,c,Objective C,C,我试图在我的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
// 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”传递给不兼容类型“void(*)(double*,double*,double*)”的参数
可能是因为睡眠不足,但我有点困惑,因为我是C语言的新手。您试图传递调用
\u fmincg\u evaluate
函数的结果,而不是传递实际的\u fmincg\u evaluate
函数
你想要:
fmincg(_fmincg_evaluate, inputFeatures->matrix, inputFeatures.elementCount, 50);
fmincg
的实现将负责调用传入的函数指针及其所需的任何参数
更新:正如阿德里安在下面的评论中指出的,无法将
\u fmincg\u evaluate
函数作为第一个参数传递给fmincg
函数,因为参数类型不匹配。您试图传递调用\u fmincg\u evaluate
函数的结果,而不是传递实际的\u fmincg\u evaluate
函数
你想要:
fmincg(_fmincg_evaluate, inputFeatures->matrix, inputFeatures.elementCount, 50);
fmincg
的实现将负责调用传入的函数指针及其所需的任何参数
更新:正如Adrian在下面的评论中指出的,您不能将
\u fmincg\u evaluate
函数作为第一个参数传递给fmincg
函数,因为参数类型不匹配。这不会导致类似的错误吗?fmincg
的第一个参数应该具有类型void(*)(double*,double*,double*)
,但是\u fmincg\u evaluate
具有类型void(*)(id,LKDataset*,LKFeature*,LKMatrixObject*,double,LKMatrixObject*)
@Adrian您是正确的。我忽略了参数上的差异。我会更新我的答案。啊,你们都对了。我继续,并修复了所有这些。谢谢你发现了!这不会导致类似的错误吗?fmincg
的第一个参数应该具有类型void(*)(double*,double*,double*)
,但是\u fmincg\u evaluate
具有类型void(*)(id,LKDataset*,LKFeature*,LKMatrixObject*,double,LKMatrixObject*)
@Adrian您是正确的。我忽略了参数上的差异。我会更新我的答案。啊,你们都对了。我继续,并修复了所有这些。谢谢你发现了!第一个代码块中的函数原型不是有效的C语法;我怀疑你在把它复制粘贴到这个问题上时犯了一个错误。你在这里要做什么也不清楚。你的第一个代码块中的函数原型不是有效的C语法;我怀疑你在把它复制粘贴到这个问题上时犯了一个错误。也不清楚你想在这里做什么。