【发布时间】:2014-12-14 03:24:00
【问题描述】:
我写了一个c++程序fllow(3.43.cpp):
#include <iostream>
using std::cout;
using std::endl;
void version_1(int **arr) {
for (const int (&p)[4] : arr) {
for (int q : p) {
cout << q << " ";
}
cout << endl;
}
}
int main() {
int arr[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
version_1(arr);
return 0;
}
然后我使用:gcc my.cpp -std=c++11 编译它,有一个我无法处理的错误。
信息:
3.43.cpp:6:30: error: no matching function for call to ‘begin(int**&)’
for (const int (&p)[4] : arr) {
^
3.43.cpp:6:30: note: candidates are:
In file included from /usr/include/c++/4.8.2/bits/basic_string.h:42:0,
from /usr/include/c++/4.8.2/string:52,
from /usr/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/include/c++/4.8.2/ios:42,
from /usr/include/c++/4.8.2/ostream:38,
from /usr/include/c++/4.8.2/iostream:39,
from 3.43.cpp:1:
/usr/include/c++/4.8.2/initializer_list:89:5: note: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)
begin(initializer_list<_Tp> __ils) noexcept
我在谷歌搜索,但没有找到类似的答案。
【问题讨论】:
-
见passing 2D array to function。此外,您在致电
version_1... -
把参数类型改成
const int (&arr)[3][4],把其他两个参数去掉。 -
基于范围的 for 循环只能用于
std::begin(x)和std::end(x)有效的对象 -
@quantdev 我删除了本地程序中的 aguments,但在这里要删除,我已经更改了它。谢谢!
-
为什么不使用一些理智的东西……比如矢量?
标签: c++ arrays c++11 compilation