【发布时间】:2014-12-04 18:12:00
【问题描述】:
当我尝试在 gcc 4.8.2 中编译以下代码时,我收到以下错误:
test.cc: In function ‘void foo(int*)’: test.cc:15:16: error: no matching function for call to ‘begin(int*&)’ for (int i : bar) { ^
与来自模板库更深处的其他人一起。
#include <iostream>
using namespace std;
void foo(int*);
int main() {
int bar[3] = {1,2,3};
for (int i : bar) {
cout << i << endl;
}
foo(bar);
}
void foo(int* bar) {
for (int i : bar) {
cout << i << endl;
}
}
如果我重新定义 foo 以使用索引 for 循环,则代码编译并按预期运行。此外,如果我将基于范围的输出循环移动到main,我也会得到预期的行为。
如何将数组bar 传递给foo,使其能够对其执行基于范围的for循环?
【问题讨论】: