c++ – Local function definition are illegal
c++ – Local function definition are illegal
You have the functions Test
and TestTwo
inside your main
function.
Define your functions outside the main
routine.
c++ – Local function definition are illegal
While local function definitions as above are illegal C++ supports local functions by means of lambdas. The following is legal in C++11 and later.
#include <iostream>
using namespace std;
int main()
{
auto TimesTwo = [](int num1)
{
int result;
result = num1 * 2;
return result;
};
auto Test = [&TimesTwo](int a)
{
int result;
int num1;
cin >> num1;
result = TimesTwo(num1);
return result;
};
using fp = int (*)(int);
fp f1 = TimesTwo; // non-capturing lambda can be converted to function pointer.
return 0;
}
A helpful feature of lambdas is that lambdas that do not capture anything can be converted to a function pointer.