binary '-': no operator found which takes a left-hand operand of type 'const CArray<int>::iterator'

286
01 февраля 2018, 18:39

У меня выскакивает ошибка при вызове 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'
Answer 1

Из сообщения компилятора видно, что должно быть

int operator-(const iterator &_rhs) const

А еще лучше

friend int operator-(const iterator &_lhs, const iterator &_rhs)
READ ALSO
Безопасность Standart Template Library [требует правки]

Безопасность Standart Template Library [требует правки]

Слышал мнение, что STL сама по себе не безопасна и использовать ее в своих проектах не стоитХочется услышать мнение профессионалов об этом...

240
Стили к собственному виджету - C++ Qt

Стили к собственному виджету - C++ Qt

Всем привет! Столкнулся с такой трудностью:

290
QLabel не заполняется из другого класса

QLabel не заполняется из другого класса

У меня приложение с картой, при нажатии на карту я рисую Waypoint и сохраняю его в мой struct WaypointData

258
Ограничения двумерного vector&#39;а

Ограничения двумерного vector'а

Подскажите, пожалуйста, есть ли у двумерного vector'а ограничение по размеруИ, если да, то какое оно в обе стороны

234