mercredi 27 mai 2015

Why can't I use a for each loop on array of int pointers in C++

I'm trying to have an array of pointers (int *a[10]) and then use a range based for loop (as in the C++11 standard). However, the compiler complains D: - it says "error: invalid initialization of reference of type ‘int&’ from expression of type ‘int*’".

My code is basically:

main(){
    int *a[10];
    for(int& e : a){}
}

I've read this question which links me to this page; by reading that, I think I understand that the container you're trying to iterate over must implement a begin() and an end() methods. I guess the primitive arrays don't implement these; is that the case? Also, is it the case that int& a = c; makes a have the same address as c, for some int c?

Is there another alternative?

Aucun commentaire:

Enregistrer un commentaire