Lambda Function returning double

Hello to everybody,

I'm trying to create a class which contains a lambda function which is defined in the main (The code is written below).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

class Function_Class
{
public:
        double (*Left)(vector<double>& x, double& phi, vector<double>& dphidx, double& u, vector<double>& dudx);
	double (*Right)(vector<double>& x, double& phi, vector<double>& dphidx, double& u, vector<double>& dudx);
	double Complete(vector<double>& x, double& phi, vector<double>& dphidx, double& u, vector<double>& dudx)
	{
	    return Left(x, phi, dphidx, u, dudx) - Right(x, phi, dphidx, u, dudx);
	};
};

int main()
{
	Function_Class.Left = [](vector<double>& x, double& phi, vector<double>& dphidx, double& u, vector<double>& dudx)
	{
	    return 2;
	};
        return 0;
}


The compiler provides a not understandable error.
Can I know if someone knows the solution?

Thank you in advance for the help,

Federico.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Function_Class {
public:
  double (*Left)(vector< double > &x, double &phi, vector< double > &dphidx,
                 double &u, vector< double > &dudx);
  double (*Right)(vector< double > &x, double &phi, vector< double > &dphidx,
                  double &u, vector< double > &dudx);
  double Complete(vector< double > &x, double &phi, vector< double > &dphidx,
                  double &u, vector< double > &dudx) {
    return Left(x, phi, dphidx, u, dudx) - Right(x, phi, dphidx, u, dudx);
  };
};

int main() {
  // Create an object of type Function_Class
  Function_Class c;
  // Modify the object:
  c.Left = [](vector< double > &x, double &phi, vector< double > &dphidx,
              double &u, vector< double > &dudx)
      -> double // If you don't specify the return type, it will be deduced from
                // the return statement (as int)
  { return 2; };

  return 0;
}
Last edited on
Thank you very much. Now it wors. So in this case, putting "return 2" in the function it returned an integer value instead of a double as written in the function inizialization.
Understood.
Thank you for the help,
Bye.
Topic archived. No new replies allowed.