samedi 28 septembre 2019

variable cannot be implicitly captured in a lambda with no capture-default specified using a switch statement

I am trying to convert a name to a number. my problem is that as I try to do a simple a=a+1 in a switch statement I get the error message "variable 'a' cannot be implicitly captured in a lambda with no capture-default specified"

looking around here for the same error message I see that I should use [], [=] or [&]. my problem seems more how and where to do so. if I go [](int a=0){}; where I initialize the variable then my message is " error: use of undeclared identifier 'a'"

here is the code with my problem

#include <jni.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;

static int nameToNumber(string fn, string ln)
{
    string nameOne = fn;
    string nameTwo = ln;
    [](int a=0){};
    int b = 0;
    int num = 0;

    for_each(nameOne.begin(), nameOne.end(), [](char &c )
    {
        c=::toupper(c);

        switch (c){

            case 'A':
            case 'J':
            case 'S': a=a+1;
                break;
            case 'B':
            case 'K':
            case 'T': a=a+2;
                break;
            case 'C':
            case 'L':
            case 'U': a=a+3;
                break;
            case 'D':
            case 'M':
            case 'V': a=a+4;
                break;
            case 'E':
            case 'N':
            case 'W': a=a+5;
                break;
            case 'F':
            case 'O':
            case 'X': a=a+6;
                break;
            case 'G':
            case 'P':
            case 'Y': a=a+7;
                break;
            case 'H':
            case 'Q':
            case 'Z': a=a+8;
                break;
            case 'I':
            case 'R': a=a+9;
                break;
            default: a=a+0;
        }
    });


    for_each(nameTwo.begin(), nameTwo.end(), [](char &c)
    {


        c=::toupper(c);
        switch (c){

            case 'A':
            case 'J':
            case 'S': b=b+1;
                break;
            case 'B':
            case 'K':
            case 'T': b=b+2;
                break;
            case 'C':
            case 'L':
            case 'U': b=b+3;
                break;
            case 'D':
            case 'M':
            case 'V': b=b+4;
                break;
            case 'E':
            case 'N':
            case 'W': b=b+5;
                break;
            case 'F':
            case 'O':
            case 'X': b=b+6;
                break;
            case 'G':
            case 'P':
            case 'Y': b=b+7;
                break;
            case 'H':
            case 'Q':
            case 'Z': b=b+8;
                break;
            case 'I':
            case 'R': b=b+9;
        }
    });

num = a + b;
if ((num > 9) && (num != 11) && (num != 22) && (num != 33))
{
//add both digits together to get a single digit
a=0;
b=0;

a = num / 10; //get first digit
b = num % 10; //get second digit
num = a + b; //add them together
}
return num;
}

and here is where I call it

extern "C" JNIEXPORT jstring JNICALL
Java_com_sezju_namenumerology_MainActivity_transformInfo(
        JNIEnv *env,
        jobject /* this */,
        jstring fName,
        jstring lName,
        jint age) {
int luckynum = nameToNumber(jstringToString(env, fName), jstringToString(env, lName));
string message = jstringToString(env, fName) + " " + jstringToString(env, lName) + ".\n";
message += "You are " + decToHexa((int)age)+ " years old in hexadecimal. \n";
message += "Your number is " + std::to_string(luckynum) + " \n";
return env->NewStringUTF(message.c_str());

my expected result would be, for a name entered to have a result of a single digit, or 11, 22, or33.

Aucun commentaire:

Enregistrer un commentaire