没有匹配的函数来调用'begin(int **&)'

火花飞

我写了一个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

我在谷歌搜索,但找不到类似的答案。

巴里

由于arr只是一个指针,因此无法推断出它的大小。但是,由于您实际上是在传递真实数组,因此您可以按其尺寸对函数进行模板化,因此您可以通过引用获取实际数组,而不必使其衰减为指针:

template <size_t X, size_t Y>
void foo(const int (&arr)[X][Y])
{
    std::cout << "Dimensions are: " << X << "x" << Y << std::endl;

    for (const int (&row)[Y] : arr) {
        for (int val : row) {
            std::cout << val << ' ';
        }
        std::cout << std::endl;
    }
}

int main() {
    int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
    foo(arr);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

没有匹配的函数来调用'regex_search(...)'

sort()-没有匹配的函数来调用'swap'

没有匹配的函数来调用sort()

没有匹配的函数来调用类构造函数

没有匹配的函数来调用“ getline”

没有匹配的函数可以调用'std :: less <int> :: less(const int&,const int&)'

错误:没有匹配的构造函数来初始化“ set <int>”

没有匹配的函数来调用'replace(std :: basic_string <char> :: iterator,std :: basic_string <char> :: iterator,char,int)'|

C ++线程-没有匹配的函数来调用

没有匹配的函数来调用“点”和“反射”

没有匹配的函数来调用“ make_shared”

错误:没有匹配的函数来调用“ make_pair”

没有匹配的函数来调用getline(std :: istream&,int&)

没有匹配的函数来调用'Kt :: Kt(int&,int)'

没有匹配的函数来调用“连接”(QT)

C ++-没有匹配的函数来调用'getaddrinfo'

没有匹配的函数来调用类中的成员函数

没有匹配的函数来调用

运算符>>错误:没有匹配的函数来调用

没有匹配的函数来调用A :: A(MainWindow * const)

没有匹配的函数来调用“ myclass :: myclass()”

没有匹配的成员函数来调用“连接”-使用“ this”时

C ++:没有匹配的函数来调用

没有匹配的函数来调用'std :: advance'错误

发生没有匹配的函数来调用C ++中的错误

没有匹配的函数来调用FMOD :: Studio :: System :: setListenerAttributes()

没有匹配的函数调用'QGridLayout::addWidget(QPushButton, int&, int&)

C++ - 在 foreach 中复制向量给出“没有匹配的函数来调用 std::vector<int>::push_back(std::vector<int>&)”

没有匹配的函数来调用“strlen”