Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new c++11 for loop causes: "error: ‘begin’ was not declared in this scope"

Tags:

c++

c++11

I'm trying to learn c++, so I wrote a short program that uses the new c++11 for loop, which makes the compiler give me an error I don't understand. this is my c++ code:

#include <iostream>
#include <cmath>
using namespace std;

float legge_oraria_moto_accelerato(float a[3]){
    return a[2]*a[0] + 0.5*a[1]*a[0]*a[0];
}
int corri(float (f)(float array[3]), float arrays[3][3])
    { for(auto i:arrays) cout << f(i) << '\n';
    return 0;
} 

int main()
{ 
return 0;
}

and this is the compiler's (g++ -std=gnu++11) error:

mezzo.cpp: In function ‘int corri(float (*)(float*), float (*)[3])’:
mezzo.cpp:9:18: error: ‘begin’ was not declared in this scope
     { for(auto i:arrays) cout << f(i) << '\n';
                  ^
mezzo.cpp:9:18: note: suggested alternatives:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
                 from /usr/include/c++/4.9/string:52,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from mezzo.cpp:1:
/usr/include/c++/4.9/initializer_list:89:5: note:   ‘std::begin’
     begin(initializer_list<_Tp> __ils) noexcept
     ^
/usr/include/c++/4.9/initializer_list:89:5: note:   ‘std::begin’
mezzo.cpp:9:18: error: ‘end’ was not declared in this scope
     { for(auto i:arrays) cout << f(i) << '\n';
                  ^
mezzo.cpp:9:18: note: suggested alternatives:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
                 from /usr/include/c++/4.9/string:52,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from mezzo.cpp:1:
/usr/include/c++/4.9/initializer_list:99:5: note:   ‘std::end’
     end(initializer_list<_Tp> __ils) noexcept
     ^
/usr/include/c++/4.9/initializer_list:99:5: note:   ‘std::end’
like image 546
bercio Avatar asked Mar 04 '15 12:03

bercio


1 Answers

Range-based for loops work with arrays, but not with pointers. The issue here is that arrays is actually a pointer and not an array.

When you have a function parameter that is declared as an array, it is adjusted to a pointer type. You can see this here with the parameter float arrays[3][3]: In the compiler error message you can see that the actual parameter type is a pointer to an array float (*)[3], which can't be used with a ranged-based for loop.

If you pass the array by reference instead (float (&arrays)[3][3]), it won't adjusted to a pointer in this manner and will therefore work with the range-based for loop.

like image 107
interjay Avatar answered Sep 16 '22 12:09

interjay