Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no matching function for call to ‘begin(int**&)’

I wrote a c++ program as 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;
}

Then I compile it by using: gcc my.cpp -std=c++11, there is an error I can not deal with. Info:

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

I search it in google, but not find similar answer.

like image 948
Sparks_Fly Avatar asked Dec 14 '14 03:12

Sparks_Fly


2 Answers

Since arr is just a pointer, there's no way to deduce how big it is. But, since you are actually passing in a real array, you can just template your function on its dimensions so you take the actual array by reference rather than having it decay to a pointer:

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);
}
like image 177
Barry Avatar answered Oct 04 '22 04:10

Barry


std::begin() and std::end() won't work for pointers. They only work for arrays. If you want to deduce the size of your array you'll need to pass it as a reference your function:

#include <cstddef>
template <std::size_t A, std::size_t B>
void version_1(int (&arr)[B][A]) {
    for (const int (&p)[A] : arr) {
        for (int q : p) {
            cout << q << " ";
        }
        cout << '\n';
    }
}
like image 35
Dietmar Kühl Avatar answered Oct 04 '22 02:10

Dietmar Kühl