I get an error when calling std :: sort

My class is CArray, to the elements of which I want to apply sorting:

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; } } } 

In main.cpp:

 CArray<int> vec; for (int i = 0; i < 20; i++) vec.push_back(i); std::sort(vec.begin(), vec.end()); 

More detailed error output

 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' 

    1 answer 1

    From the compiler message you can see what should be

     int operator-(const iterator &_rhs) const 

    Better yet

     friend int operator-(const iterator &_lhs, const iterator &_rhs) 
    • Thanks you! The problem was solved, if only with the first option add iterator operator- (const int & _rhs) const {return iterator (ar, index - _rhs); } - Elnur Ismailzada
    • @Elnur Ismailzada: No This problem was solved exactly as I said. And everything else is a completely different "problem". And if you are going to implement your iterator from scratch, then you still have a whole string of "problems" waiting for you: you will have to implement all the required operations. And - , + and < , and == , and != And everything else. - AnT