Writing recursive C++ lambdas giving error...!!

Revision en2, by Spartan-007, 2024-08-31 20:04:47

Hello Codeforcers!

I'm encountering an issue with a recursive lambda DFS function in my code. Everything was working fine before, but now, after adding this function, I get an error. Can someone help me debug this and find out what's going wrong?

Thanks!

Code :-

    int n; cin >> n;
    vector<int> g[n];
    for(int i=1; i<n; ++i) {
        int u, v; cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    string s; cin >> s;

    vector<int> leaf;
    auto dfs = [&](auto &&self, int u, int par) {
        for(auto &x : g[u]){
            if(x==par) continue;
            self(self, x, u);
        }
        if(g[u].size() == 1) leafs.push_back(u);
    };
    dfs(dfs,0,-1);

Error :

E:\Programming\cp\Sublime_Files\Hi.cpp: In function 'void solve()':
E:\Programming\cp\Sublime_Files\Hi.cpp:90:16: error: use of deleted function 'solve()::<lambda(auto:1&, ll, ll)>::~<lambda>()'
     auto dfs = [&](auto &self, ll u,ll par) {
                ^
E:\Programming\cp\Sublime_Files\Hi.cpp:90:18: note: 'solve()::<lambda(auto:1&, ll, ll)>::~<lambda>()' is implicitly deleted because the default definition would be ill-formed:
     auto dfs = [&](auto &self, ll u,ll par) {
                  ^
[Finished in 283ms]
Tags lambda expressions, c++

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English Spartan-007 2024-08-31 20:04:47 2 Tiny change: 'for(int i=0; i<n; ++i' -> 'for(int i=1; i<n; ++i'
en1 English Spartan-007 2024-08-31 20:03:52 1382 Initial revision (published)