新的c ++ 11 for循环原因:“错误:未在此范围内声明'开始'”; [英] new c++11 for loop causes: "error: ‘begin’ was not declared in this scope"

查看:276
本文介绍了新的c ++ 11 for循环原因:“错误:未在此范围内声明'开始'”;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

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

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’


推荐答案

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

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.

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

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] ),它不会以这种方式调整为指针,因此可以正常工作使用基于范围的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.

这篇关于新的c ++ 11 for循环原因:“错误:未在此范围内声明'开始'”;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆