I'm writing a class that encapsulates a 2-dimensional array. This is the copy constructor. (WIDTH and HEIGHT are compile-time constants, which is why I saw it fit to use arrays.)
MyClass::MyClass(const MyClass &other)
{
std::copy(
&array[0][0], &array[0][0] + WIDTH*HEIGHT,
&other.array[0][0]);
}
I'm using a method that is correct according to this question, and worked before I changed the prototype to be const & rather than simple pass-by-value. However, I now receive this compiler error:
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
from /usr/include/c++/4.8/string:40,
from MyClass.hpp:4,
from MyClass.cpp:1:
/usr/include/c++/4.8/bits/stl_algobase.h: In instantiation of ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = ArrayDataType*; _OI = const ArrayDataType*]’:
/usr/include/c++/4.8/bits/stl_algobase.h:428:38: required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = ArrayDataType*; _OI = const ArrayDataType*]’
/usr/include/c++/4.8/bits/stl_algobase.h:460:17: required from ‘_OI std::copy(_II, _II, _OI) [with _II = ArrayDataType*; _OI = const ArrayDataType*]’
MyClass.cpp:17:28: required from here
/usr/include/c++/4.8/bits/stl_algobase.h:390:70: error: no matching function for call to ‘std::__copy_move<false, true, std::random_access_iterator_tag>::__copy_m(ArrayDataType*&, ArrayDataType*&, const ArrayDataType*&)’
_Category>::__copy_m(__first, __last, __result);
^
/usr/include/c++/4.8/bits/stl_algobase.h:390:70: note: candidate is:
/usr/include/c++/4.8/bits/stl_algobase.h:368:9: note: template<class _Tp> static _Tp* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(const _Tp*, const _Tp*, _Tp*) [with _Tp = _Tp; bool _IsMove = false]
__copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
^
/usr/include/c++/4.8/bits/stl_algobase.h:368:9: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/stl_algobase.h:390:70: note: deduced conflicting types for parameter ‘_Tp’ (‘ArrayDataType’ and ‘const ArrayDataType’)
_Category>::__copy_m(__first, __last, __result);
I'd assume that the C++ standard wouldn't make std::copy unusable in a copy constructor by constant reference in this fashion, so what am I doing wrong here?
Aucun commentaire:
Enregistrer un commentaire