I am just exploring lambda expression and trying to use it to return from a function using C++ ternary operator. However the below code does not work with error!
this is a simplified version of the program to explain my actual problem so kindly post answer using a lambda only, not just return (x+y+offset).
In member function 'int Calculator::add(int, int)': 12:26: error: no match for ternary 'operator?:' (operand types are 'bool', 'int', and 'Calculator::add(int, int)::<lambda()>') 16:3: warning: control reaches end of non-void function [-Wreturn-type]
#include <iostream>
#include <string>
class Calculator
{
public:
bool initalized = false;
int offset = 0;
int add(int x, int y)
{
return !initalized ? -1 : [this, x, y]()
{
return ((x+y) + offset);
};
}
};
int main()
{
Calculator c;
c.initalized = true;
c.offset = 1;
printf("%d", c.add(2,10));
}
What am I doing wrong here?
Aucun commentaire:
Enregistrer un commentaire