samedi 4 novembre 2017

Error in nested c++11 for loop: binding lvalue reference to rvalue

When I try to compile my code containing this fragment:

vector<vector<bool>> p(n);
for(vector<bool>& i : p) {
    i = vector<bool>(m);
}

for(vector<bool>& i : p) {
        for(bool& j : i) {
            int a = round(rand() / double(RAND_MAX));
            if(a) j = true;
            else j = false;
        }
}

occurs an error saying that j is an rvalue so I can't bind lvalue reference to it. Why is it rvalue? If I'm getting it right, it's not an expression, but existing in memory element of vector i. What's more, it works just fine when I use only one loop.

I know that I can easily replace that with normal for loop, but I want to understand why that's wrong and learn if I can use nested c++11 for loop when I want to change values.

Aucun commentaire:

Enregistrer un commentaire