После int ia[3][4]{}
я запускаю:
for(auto row : ia) // row should be type int*
for(int *j = std::begin(*row); j!= end(*row); ++j) // error!!
std::cout << *j << std::endl;
В соответствии с праймером C ++ 5:
Поскольку строка не является ссылкой, когда компилятор
инициализирует строкуон преобразует каждый элемент массива (как и любой другой
объект типа массива)
в указатель на первый элемент этого массива
Так что, если строка является указателем на ia
первый элемент, то почему происходит ошибка?
Большое спасибо!
ОБНОВЛЕНИЕ:
ch339.cpp: In function ‘int main()’:
ch339.cpp:10:31: error: no matching function for call to ‘begin(int*&)’
for (int *j = begin(row); j!= end(row); ++j )
^
ch339.cpp:10:31: note: candidates are:
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 ch339.cpp:1:
/usr/include/c++/4.9/initializer_list:89:5: note: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)
begin(initializer_list<_Tp> __ils) noexcept
^
/usr/include/c++/4.9/initializer_list:89:5: note: template argument deduction/substitution failed:
ch339.cpp:10:31: note: mismatched types ‘std::initializer_list<_Tp>’ and ‘int*’
for (int *j = begin(row); j!= end(row); ++j )
^
In file included from /usr/include/c++/4.9/string:51:0,
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 ch339.cpp:1:
/usr/include/c++/4.9/bits/range_access.h:87:5: note: template<class _Tp, long unsigned int _Nm> _Tp* std::begin(_Tp (&)[_Nm])
begin(_Tp (&__arr)[_Nm])
^
/usr/include/c++/4.9/bits/range_access.h:87:5: note: template argument deduction/substitution failed:
ch339.cpp:10:31: note: mismatched types ‘_Tp [_Nm]’ and ‘int*’
for (int *j = begin(row); j!= end(row); ++j )
^
In file included from /usr/include/c++/4.9/string:51:0,
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 ch339.cpp:1:
/usr/include/c++/4.9/bits/range_access.h:58:5: note: template<class _Container> decltype (__cont.begin()) std::begin(const _Container&)
begin(const _Container& __cont) -> decltype(__cont.begin())
^
/usr/include/c++/4.9/bits/range_access.h:58:5: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of ‘template<class _Container> decltype (__cont.begin()) std::begin(const _Container&) [with _Container = int*]’:
ch339.cpp:10:31: required from here
/usr/include/c++/4.9/bits/range_access.h:58:5: error: request for member ‘begin’ in ‘__cont’, which is of non-class type ‘int* const’
/usr/include/c++/4.9/bits/range_access.h:48:5: note: template<class _Container> decltype (__cont.begin()) std::begin(_Container&)
begin(_Container& __cont) -> decltype(__cont.begin())
^
/usr/include/c++/4.9/bits/range_access.h:48:5: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of ‘template<class _Container> decltype (__cont.begin()) std::begin(_Container&) [with _Container = int*]’:
ch339.cpp:10:31: required from here
/usr/include/c++/4.9/bits/range_access.h:48:5: error: request for member ‘begin’ in ‘__cont’, which is of non-class type ‘int*’
ch339.cpp:10:45: error: no matching function for call to ‘end(int*&)’
for (int *j = begin(row); j!= end(row); ++j )
^
ch339.cpp:10:45: note: candidates are:
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 ch339.cpp:1:
/usr/include/c++/4.9/initializer_list:99:5: note: template<class _Tp> constexpr const _Tp* std::end(std::initializer_list<_Tp>)
end(initializer_list<_Tp> __ils) noexcept
^
/usr/include/c++/4.9/initializer_list:99:5: note: template argument deduction/substitution failed:
ch339.cpp:10:45: note: mismatched types ‘std::initializer_list<_Tp>’ and ‘int*’
for (int *j = begin(row); j!= end(row); ++j )
^
In file included from /usr/include/c++/4.9/string:51:0,
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 ch339.cpp:1:
/usr/include/c++/4.9/bits/range_access.h:97:5: note: template<class _Tp, long unsigned int _Nm> _Tp* std::end(_Tp (&)[_Nm])
end(_Tp (&__arr)[_Nm])
^
/usr/include/c++/4.9/bits/range_access.h:97:5: note: template argument deduction/substitution failed:
ch339.cpp:10:45: note: mismatched types ‘_Tp [_Nm]’ and ‘int*’
for (int *j = begin(row); j!= end(row); ++j )
^
In file included from /usr/include/c++/4.9/string:51:0,
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 ch339.cpp:1:
/usr/include/c++/4.9/bits/range_access.h:78:5: note: template<class _Container> decltype (__cont.end()) std::end(const _Container&)
end(const _Container& __cont) -> decltype(__cont.end())
^
/usr/include/c++/4.9/bits/range_access.h:78:5: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of ‘template<class _Container> decltype (__cont.end()) std::end(const _Container&) [with _Container = int*]’:
ch339.cpp:10:45: required from here
/usr/include/c++/4.9/bits/range_access.h:78:5: error: request for member ‘end’ in ‘__cont’, which is of non-class type ‘int* const’
/usr/include/c++/4.9/bits/range_access.h:68:5: note: template<class _Container> decltype (__cont.end()) std::end(_Container&)
end(_Container& __cont) -> decltype(__cont.end())
^
/usr/include/c++/4.9/bits/range_access.h:68:5: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of ‘template<class _Container> decltype (__cont.end()) std::end(_Container&) [with _Container = int*]’:
ch339.cpp:10:45: required from here
/usr/include/c++/4.9/bits/range_access.h:68:5: error: request for member ‘end’ in ‘__cont’, which is of non-class type ‘int*’
shell returned 1
Press ENTER or type command to continue