0

i'm not such familiar with lambda expresion. Is there any chances to call reflexie in lbd and lbd in reflexie? I get the error in reflexie: 'lbd is not captured' Thank you in advanced

auto reflexie = [&tab,nrl,o,nrc,nro,lbd](int x, int y,int dirx,int diry) -> void {
        x+=dirx;
        y+=diry;

        for(int k = 0; k < nro; k++)
        {
            tab[o[k].p.l][o[k].p.c]=o[k].t;
        }

        while(x>=0 && x<nrl && y>=0 && y<nrc){
            if(tab[x][y]=='@' || tab[x][y] =='v' || tab[x][y] =='>' || tab[x][y] =='<' || tab[x][y] =='^'||tab[x][y] =='X'||tab[x][y] =='*') break;
            if(tab[x][y]=='_' && tab[x-1][y-1]=='#'){lbd(x,y,-1,+1);}

            tab[x][y]='#';
            x+=dirx;
            y+=diry;

        }
    };

     auto lbd = [&tab,nrl,o,s,nrc,nro,reflexie](int x, int y,int dirx,int diry) -> void {

        x+=dirx;
        y+=diry;


        int a = x;
        while(x>=0 && x<nrl && y>=0 && y<nrc){
            if(tab[x][y]=='@' || tab[x][y] =='v' || tab[x][y] =='>' || tab[x][y] =='<' || tab[x][y] =='^' || tab[x][y] =='X'||tab[x][y] =='*') break;

            if(tab[x][y]=='_' || tab[x][y]=='|') {
            if(tab[x][y]=='_' && (tab[x][y-1]=='#' || tab[x][y+1]=='#')){break;}
            if(tab[x][y]=='|' && (tab[x][y-1]=='#' || tab[x][y+1]=='#')){break;}
            if(tab[x][y]=='|' && tab[x+1][y-1]=='#'){reflexie(x,y,-1,-1);}

            if(tab[x][y]=='_' && tab[x+1][y+1]=='#'){reflexie(x,y,+1,-1);}

            break;
            }
            tab[x][y]='#';
            x+=dirx;
            y+=diry;

        }
    };
4
  • You're trying to capture lbd before it is declare/defined. Commented Apr 3, 2020 at 10:52
  • Could you give me an idea of workaround? Commented Apr 3, 2020 at 11:02
  • @jrok's answer seems to work Commented Apr 3, 2020 at 11:08
  • The boring idea for production code would be: don't do this unless it actually solves something, use lambdas for simple glue code. Boring, I know, but that's how I like my code, especially if I have to debug it or solve compiler errors. Commented Apr 3, 2020 at 11:42

1 Answer 1

1

You could store the second lambda in an std::function object that's defined before the first lambda. Here's a simplified example:

#include <functional>
#include <iostream>

int main()
{
    int i = 10;
    std::function<void()> g;

    auto f = [&g]() { std::cout << 'f'; g(); }; // needs to be captured by reference,
    g = [f, &i]() { std::cout << 'g'; if (--i) f(); };

    f();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.