This code works without problem:
#include "iostream"
#include "functional"
std::function<double (double)> retFun(double* a) {
return [a](double x) { return x+*a; };
}
int main(){
double*e = new double(3);
std::cout << retFun(e)(3) << std::endl;}
but if I declare retFun in an object:
.h
class InternalVariables : public Page
{
private:
std::function<double ()> retFun(double* a);
};
.cpp
std::function<double ()> InternalVariables::retFun(double *a)
{
return [a](double b){ return b+ *a;};
}
I obtain the following error
error: could not convert ‘InternalVariables::retFun(double*)::__lambda44{a}’ from ‘InternalVariables::retFun(double*)::__lambda44’ to ‘std::function’ return [a](double b){ return b+ *a;};
std::function<double ()>means that your wrapped function object will take no arguments and return adouble.[a](double b){ return b+ *a;}is a lambda that takes adoubleand returns adouble. The two signatures are mismatched.You should change your return type to
std::function<double (double)>.Also:
Standard Library headers should be included with
<...>. Example:<iostream>.There's no need to allocate a
doubleon the heap to get a pointer to it. Just use&on adoubleon the stack. Allocations in C++11 should be always done through smart pointers, do not usenew/delete.You might want to simply return
autoinstead ofstd::function. The latter is a type-erased wrapper over any function object. Lambdas have their own anonymous type.