新的 c++11 for 循环导致:“错误:'begin' 未在此范围内声明”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28853974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:05:27  来源:igfitidea点击:

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

c++c++11

提问by bercio

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:

我正在尝试学习c++,所以我写了一个使用新的c++11 for循环的简短程序,这使得编译器给我一个我不明白的错误。这是我的 C++ 代码:

#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:

这是编译器的 (g++ -std=gnu++11) 错误:

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'

回答by interjay

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

基于范围的 for 循环适用于数组,但不适用于指针。这里的问题是它arrays实际上是一个指针而不是一个数组。

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.

当您有一个声明为数组的函数参数时,它会被调整为指针类型。您可以在此处通过参数看到这一点float arrays[3][3]:在编译器错误消息中,您可以看到实际参数类型是指向数组的指针float (*)[3],它不能与基于范围的 for 循环一起使用。

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.

如果您改为通过引用传递数组 ( float (&arrays)[3][3]),它将不会以这种方式调整为指针,因此将与基于范围的 for 循环一起使用。