我正在尝试使用来返回函数,std::function<type>
但遇到输入问题...请参见代码:
#include "LossFunction.hpp"
LossFunction::LossFunction() {
}
LossFunction::LossFunction(int functionType) {
this->functionType = functionType;
this->fun = this->getLossFunction();
}
LossFunction::~LossFunction() {
}
std::function<float(Input,Output)> LossFunction::getLossFunction() {
switch (this->functionType){
case 0:
return this->f1;
break;
default:
return this->f1;
break;
}
}
编译时出现以下错误:
LossFunction.cpp: In member function ‘std::function<float(Input, Output)> LossFunction::getLossFunction()’:
LossFunction.cpp:18:17: error: cannot convert ‘LossFunction::f1’ from type ‘float (LossFunction::)(Input, Output)’ to type ‘std::function<float(Input, Output)>’
return this->f1;
^
LossFunction.cpp:21:17: error: cannot convert ‘LossFunction::f1’ from type ‘float (LossFunction::)(Input, Output)’ to type ‘std::function<float(Input, Output)>’
return this->f1;
问题不在于名称空间,而在于独立功能和成员功能之间的区别。独立的函数可以传递给std::function
的构造函数,但成员函数需要bind
其第一个参数中的a,如下所示:
switch (this->functionType){
case 0:
return std::bind(&LossFunction::f1, this, _1, _2);
break;
default:
return std::bind(&LossFunction::f1, this, _1, _2);
break;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句