У меня выскакивает ошибка при вызове std::sort
Мой класс CArray, к элементам которого я и хочу применить сортировку:
template <typename TData>
class CArray
{
...
class iterator:
public std::iterator<std::random_access_iterator_tag, TData>
{
...
int operator-(const iterator &_rhs)
{
return index - _rhs.index;
}
}
}
В main.cpp:
CArray<int> vec;
for (int i = 0; i < 20; i++) vec.push_back(i);
std::sort(vec.begin(), vec.end());
Более подробный вывод ошибки
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): error C2678: binary '-': no operator found which takes a left-hand operand of type 'const CArray<int>::iterator' (or there is no acceptable conversion)
1>d:\github\carraytemplate\carraytemplate\carraytemplate\array.h(141): note: could be 'int CArray<int>::iterator::operator -(const CArray<int>::iterator)'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): note: while trying to match the argument list '(const CArray<int>::iterator, const CArray<int>::iterator)'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3218): note: see reference to function template instantiation 'void std::sort<_RanIt,std::less<void>>(_RanIt,_RanIt,_Pr)' being compiled
1> with
1> [
1> _RanIt=CArray<int>::iterator,
1> _Pr=std::less<void>
1> ]
1>d:\github\carraytemplate\carraytemplate\carraytemplate\carraytemplate.cpp(21): note: see reference to function template instantiation 'void std::sort<CArray<int>::iterator>(_RanIt,_RanIt)' being compiled
1> with
1> [
1> _RanIt=CArray<int>::iterator
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): error C2672: '_Sort_unchecked': no matching overloaded function found
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): error C2780: 'void std::_Sort_unchecked(_RanIt,_RanIt,_Diff,_Pr)': expects 4 arguments - 3 provided
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3173): note: see declaration of 'std::_Sort_unchecked'
Из сообщения компилятора видно, что должно быть
int operator-(const iterator &_rhs) const
А еще лучше
friend int operator-(const iterator &_lhs, const iterator &_rhs)
Современные инструменты для криптотрейдинга: как технологии помогают принимать решения
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости