angry_pikachu's blog

By angry_pikachu, history, 2 years ago, In English

I am getting error

prog.cpp: In lambda function:

prog.cpp:118:28: error: use of ‘dfs’ before deduction of ‘auto’

118 | for(ll it: nde[i]) dfs(it,fren[i]);

In this code , ll means long long, nde[i] is a vector of int

auto dfs = [&](int i,int x)->void{
        fren[i]+=x;
        for(ll it: nde[i]) dfs(it,fren[i]);
    };
dfs(0,0);

Writing a blog in codeforces for first time

C++ 17 online compiler

Worked using

    function<void(int, int)> dfs = [&](int i, int x) -> void {
        fren[i] += x;
        for (ll it : nde[i])
            dfs(it, fren[i]);
};

Also by changing vector<int> nde[n] to vector<vector<int>> nde(n) and by using the function

    auto dfs = [&](int i,int x, auto &&dfs)->void{
        fren[i]+=x;
        for(ll it: nde[i]) dfs(it,fren[i], dfs);
    };
    dfs(0,0,dfs);
  • Vote: I like it
  • -21
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
  Vote: I like it +4 Vote: I do not like it

Correct it to this

auto dfs = [&](int i,int x, auto &&dfs)->void{
        fren[i]+=x;
        for(ll it: nde[i]) dfs(it,fren[i], dfs);
    };
dfs(0,0,dfs);

  • »
    »
    2 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Getting this error

    prog.cpp: In function ‘void solve()’:

    prog.cpp:116:16: error: use of deleted function ‘solve()::<lambda(int, int, auto:22&&)>::~()’

    116 | auto dfs = [&](int i,int x, auto &&dfs)->void{

    117 | fren[i]+=x;

    118 | for(ll it: nde[i]) dfs(it,fren[i], dfs);

    119 | };

    prog.cpp:116:18: note: ‘solve()::<lambda(int, int, auto:22&&)>::~()’ is implicitly deleted because the default definition would be ill-formed:

    116 | auto dfs = [&](int i,int x, auto &&dfs)->void{

    |

    Have to edit the comment due to multi line issue :(

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      can you share the whole code?

      I think in the above code ll (long long) is causing the issue as it is getting typecasted to int as a reference argument.

      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it -8 Vote: I do not like it

        Yes

        #include <bits/stdc++.h>
        using namespace std;
        /* DEBUGGING */
        void __print(int x) { cerr << x; }
        void __print(long x) { cerr << x; }
        void __print(long long x) { cerr << x; }
        void __print(unsigned x) { cerr << x; }
        void __print(unsigned long x) { cerr << x; }
        void __print(unsigned long long x) { cerr << x; }
        void __print(float x) { cerr << x; }
        void __print(double x) { cerr << x; }
        void __print(long double x) { cerr << x; }
        void __print(char x) { cerr << '\'' << x << '\''; }
        void __print(const char *x) { cerr << '\"' << x << '\"'; }
        void __print(const string &x) { cerr << '\"' << x << '\"'; }
        void __print(bool x) { cerr << (x ? "true" : "false"); }
        template <typename T, typename V>
        void __print(const pair<T, V> &x) {
            cerr << '{';
            __print(x.first);
            cerr << ',';
            __print(x.second);
            cerr << '}';
        }
        template <typename T>
        void __print(const T &x) {
            int f = 0;
            cerr << '{';
            for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
            cerr << "}";
        }
        void _print() { cerr << "]\n"; }
        template <typename T, typename... V>
        void _print(T t, V... v) {
            __print(t);
            if (sizeof...(v)) cerr << ", ";
            _print(v...);
        }
        #ifndef ONLINE_JUDGE
        #define deb(x...)                 \
            cerr << "[" << #x << "] = ["; \
            _print(x)
        #else
        #define deb(x...)
        #endif
        /* MACROS */
        typedef long long int ll;
         
        typedef pair<int, int> pii;
        typedef pair<ll, ll> pll;
         
        typedef vector<int> vi;
        typedef vector<ll> vll;
        typedef vector<pii> vpii;
        typedef vector<pll> vpll;
         
        typedef map<int, int> mii;
        typedef tuple<int, int, int> tup;
         
        #define ff first
        #define ss second
        #define pb push_back
        #define IOS                           \
            ios_base::sync_with_stdio(false); \
            cin.tie(NULL);                    \
            cout.tie(NULL);
        struct custom_hash {
            static uint64_t splitmix64(uint64_t x) {
                // http://xorshift.di.unimi.it/splitmix64.c
                x += 0x9e3779b97f4a7c15;
                x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
                x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
                return x ^ (x >> 31);
            }
        
            size_t operator()(uint64_t x) const {
                static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
                return splitmix64(x + FIXED_RANDOM);
            }
        };
        // eg - unordered_map<long long, int, custom_hash> safe_map; // for safe hash
        #define lb lower_bound
        #define ub upper_bound
        #define all(x) (x).begin(), (x).end()
        #define sz(x) (x).size()
        #define ps(x, y) fixed << setprecision(y) << x
        #define setbit(x) __builtin_popcountll(x)
        #define rep(i, a, b) for (int i = a; i < b; ++i)
        #define repr(i, a, b) for (int i = a; i >= b; --i)
         
        /* CONSTANTS */
        #define PI 3.141592653589793
        const ll MOD = 1e9 + 7;
        const ll INF = 1000000000;
        const ll MAX_N = 2e5 + 2;
        ll add(ll x, ll y) {ll res=x+y; return(res>=MOD?res-MOD:res);}
        ll mul(ll x, ll y) {ll res=x*y; return(res>=MOD?res%MOD:res);}
        ll sub(ll x, ll y) {ll res=x-y; return(res<0?res+MOD:res);}
        ll power(ll a,ll b,ll m=MOD){ ll ans=1; a=a%m;  while(b>0) {  if(b&1)  ans=(1ll*a*ans)%m; b>>=1;a=(1ll*a*a)%m;}return ans;}
        ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
        ll lcm( ll x, ll y) { return (x*y)/gcd(x,y);}
        bool isprime(ll n){if(n < 2) return 0; ll i = 2; while(i*i <= n){if(n%i == 0) return 0; i++;} return 1;}
         
        void solve() {
            ll n; cin >> n;
            vll en(n), fren;  rep(i,0,n)cin>>en[i];
            fren = en;
            vll nde[n];
            rep(i,0,n-1){
                ll x, y; cin>>x>>y;
                x--,y--;
                nde[x].pb(y);
                if(en[x])fren[y]++;
                if(en[y])fren[x]++;
            }
            auto dfs = [&](int i,int x)->void{
                fren[i]+=x;
                for(ll it: nde[i]) dfs(it,fren[i]);
            };
            dfs(0,0);
            ll q; cin >> q;
            while(q--){
                ll x, t, k; cin>>x>>t>>k;
            }
            rep(i,0,n) cout<<fren[i]<<" "; cout<<'\n';
        }
         
        int main() {
            IOS;
            ll t = 1;
            // cin >> t;
            for(int i = 1; i <= t; i++){
                //cout << "Case #<< i << " ";
                solve();
            }
        }
        
        • »
          »
          »
          »
          »
          2 years ago, # ^ |
            Vote: I like it +11 Vote: I do not like it

          Try removing the VLA. A lot of people I know face this same error because they use VLAs. More precisely, do something about the vll nde[n]; and convert it to a vector of vectors. VLAs are gcc extensions and are discouraged, and are not legal C++ at all.

          • »
            »
            »
            »
            »
            »
            2 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            vector<vll> nde(n);

            I have used this, still same error :(

            • »
              »
              »
              »
              »
              »
              »
              2 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              You also have to do the auto&& dfs change that was mentioned in the top level comment.

»
2 years ago, # |
Rev. 2   Vote: I like it +12 Vote: I do not like it

Try this

function<void(int, int)> dfs = [&](int i, int x) -> void {
        fren[i] += x;
        for (ll it : nde[i])
            dfs(it, fren[i]);
};