c++ - std::sort 用于 vector 在排序自定义对象时引发错误

标签 c++ sorting opencv vector stl

我正在尝试使用 std::sort 对已定义对象的 vector 进行排序。但是,我遇到了一长串错误。我试图从日志中找出一些东西并进行更改。我相信比较运算符必须有一些东西。有什么可以从日志中发现的吗?!

std::vector<ShapeDescriptor> bins;
bins = objects.getBinsInRange(DEFAULT_BIN, cWest, endNorth);
// sort using a custom function object
std::sort(bins.begin(), bins.end(), 
[](ShapeDescriptor a, ShapeDescriptor b)
{return a.getPosition().x < b.getPosition().x;});

错误日志是

g++ -g -Wall -std=c++17 -o main  main.cpp Detector.cpp Shape.cpp `pkg-config --cflags --libs opencv `
In file included from /usr/include/c++/7/functional:64:0,
                 from Detector.h:5,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_algo.h: In instantiation of ‘void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’:
/usr/include/c++/7/bits/stl_algo.h:1885:25:   required from ‘void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1971:31:   required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:4868:18:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)>]’
Detector.cpp:66:100:   required from here
/usr/include/c++/7/bits/stl_algo.h:1852:17: error: use of deleted function ‘ShapeDescriptor& ShapeDescriptor::operator=(ShapeDescriptor&&)’
        *__first = _GLIBCXX_MOVE(__val);
                 ^
In file included from Detector.h:7:0,
                 from Detector.cpp:1:
Shape.h:31:7: note: ‘ShapeDescriptor& ShapeDescriptor::operator=(ShapeDescriptor&&)’ is implicitly deleted because the default definition would be ill-formed:
 class ShapeDescriptor {
       ^~~~~~~~~~~~~~~
Shape.h:31:7: error: non-static const member ‘const double ShapeDescriptor::SQUARE_RATIO_L_LIMIT’, can’t use default assignment operator
Shape.h:31:7: error: non-static const member ‘const double ShapeDescriptor::SQUARE_RATIO_U_LIMIT’, can’t use default assignment operator
In file included from /usr/include/c++/7/bits/stl_algo.h:61:0,
                 from /usr/include/c++/7/functional:64,
                 from Detector.h:5,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_heap.h: In instantiation of ‘void std::__pop_heap(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’:
/usr/include/c++/7/bits/stl_algo.h:1675:19:   required from ‘void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1933:25:   required from ‘void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1948:27:   required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1968:25:   required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:4868:18:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)>]’
Detector.cpp:66:100:   required from here
/usr/include/c++/7/bits/stl_heap.h:252:17: error: use of deleted function ‘ShapeDescriptor& ShapeDescriptor::operator=(ShapeDescriptor&&)’
       *__result = _GLIBCXX_MOVE(*__first);
                 ^
In file included from /usr/include/c++/7/memory:62:0,
                 from Detector.h:4,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h: In instantiation of ‘void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >]’:
/usr/include/c++/7/bits/stl_algo.h:84:20:   required from ‘void std::__move_median_to_first(_Iterator, _Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1921:34:   required from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1953:38:   required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1968:25:   required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:4868:18:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)>]’
Detector.cpp:66:100:   required from here
/usr/include/c++/7/bits/stl_algobase.h:148:11: error: no matching function for call to ‘swap(ShapeDescriptor&, ShapeDescriptor&)’
       swap(*__a, *__b);
       ~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_pair.h:59:0,
                 from /usr/include/c++/7/bits/stl_algobase.h:64,
                 from /usr/include/c++/7/memory:62,
                 from Detector.h:4,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/move.h:187:5: note: candidate: template<class _Tp> typename std::enable_if<std::__and_<std::__not_<std::__is_tuple_like<_Tp> >, std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> >::value>::type std::swap(_Tp&, _Tp&)
     swap(_Tp& __a, _Tp& __b)
     ^~~~
/usr/include/c++/7/bits/move.h:187:5: note:   template argument deduction/substitution failed:
/usr/include/c++/7/bits/move.h: In substitution of ‘template<class _Tp> typename std::enable_if<std::__and_<std::__not_<std::__is_tuple_like<_Tp> >, std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> >::value>::type std::swap(_Tp&, _Tp&) [with _Tp = ShapeDescriptor]’:
/usr/include/c++/7/bits/stl_algobase.h:148:11:   required from ‘void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >]’
/usr/include/c++/7/bits/stl_algo.h:84:20:   required from ‘void std::__move_median_to_first(_Iterator, _Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1921:34:   required from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1953:38:   required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1968:25:   required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:4868:18:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)>]’
Detector.cpp:66:100:   required from here
/usr/include/c++/7/bits/move.h:187:5: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
/usr/include/c++/7/bits/stl_algobase.h: In instantiation of ‘void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >]’:
/usr/include/c++/7/bits/stl_algo.h:84:20:   required from ‘void std::__move_median_to_first(_Iterator, _Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1921:34:   required from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1953:38:   required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1968:25:   required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:4868:18:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)>]’
Detector.cpp:66:100:   required from here
/usr/include/c++/7/bits/move.h:210:5: note: candidate: template<class _Tp, long unsigned int _Nm> typename std::enable_if<std::__is_swappable<_Tp>::value>::type std::swap(_Tp (&)[_Nm], _Tp (&)[_Nm])
     swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
     ^~~~
/usr/include/c++/7/bits/move.h:210:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/7/memory:62:0,
                 from Detector.h:4,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:148:11: note:   mismatched types ‘_Tp [_Nm]’ and ‘ShapeDescriptor’
       swap(*__a, *__b);
       ~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/7/memory:62,
                 from Detector.h:4,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_pair.h:490:5: note: candidate: template<class _T1, class _T2> typename std::enable_if<std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value>::type std::swap(std::pair<_T1, _T2>&, std::pair<_T1, _T2>&)
     swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
     ^~~~
/usr/include/c++/7/bits/stl_pair.h:490:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/7/memory:62:0,
                 from Detector.h:4,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:148:11: note:   ‘ShapeDescriptor’ is not derived from ‘std::pair<_T1, _T2>’
       swap(*__a, *__b);
       ~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/7/memory:62,
                 from Detector.h:4,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_pair.h:498:5: note: candidate: template<class _T1, class _T2> typename std::enable_if<(! std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value)>::type std::swap(std::pair<_T1, _T2>&, std::pair<_T1, _T2>&) <deleted>
     swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
     ^~~~
/usr/include/c++/7/bits/stl_pair.h:498:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/7/memory:62:0,
                 from Detector.h:4,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:148:11: note:   ‘ShapeDescriptor’ is not derived from ‘std::pair<_T1, _T2>’
       swap(*__a, *__b);
       ~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algo.h:61:0,
                 from /usr/include/c++/7/functional:64,
                 from Detector.h:5,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_heap.h: In instantiation of ‘void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Distance = long int; _Tp = ShapeDescriptor; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’:
/usr/include/c++/7/bits/stl_heap.h:342:22:   required from ‘void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1672:23:   required from ‘void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1933:25:   required from ‘void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1948:27:   required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1968:25:   required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:4868:18:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)>]’
Detector.cpp:66:100:   required from here
/usr/include/c++/7/bits/stl_heap.h:225:29: error: use of deleted function ‘ShapeDescriptor& ShapeDescriptor::operator=(ShapeDescriptor&&)’
    *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
                             ^
/usr/include/c++/7/bits/stl_heap.h:231:29: error: use of deleted function ‘ShapeDescriptor& ShapeDescriptor::operator=(ShapeDescriptor&&)’
    *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
                             ^
In file included from /usr/include/c++/7/memory:62:0,
                 from Detector.h:4,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h: In instantiation of ‘static _BI2 std::__copy_move_backward<true, false, std::random_access_iterator_tag>::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1 = ShapeDescriptor*; _BI2 = ShapeDescriptor*]’:
/usr/include/c++/7/bits/stl_algobase.h:588:58:   required from ‘_BI2 std::__copy_move_backward_a(_BI1, _BI1, _BI2) [with bool _IsMove = true; _BI1 = ShapeDescriptor*; _BI2 = ShapeDescriptor*]’
/usr/include/c++/7/bits/stl_algobase.h:598:5:   required from ‘_BI2 std::__copy_move_backward_a2(_BI1, _BI1, _BI2) [with bool _IsMove = true; _BI1 = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _BI2 = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >]’
/usr/include/c++/7/bits/stl_algobase.h:668:48:   required from ‘_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1 = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _BI2 = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >]’
/usr/include/c++/7/bits/stl_algo.h:1851:8:   required from ‘void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1885:25:   required from ‘void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:1971:31:   required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’
/usr/include/c++/7/bits/stl_algo.h:4868:18:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)>]’
Detector.cpp:66:100:   required from here
/usr/include/c++/7/bits/stl_algobase.h:548:18: error: use of deleted function ‘ShapeDescriptor& ShapeDescriptor::operator=(ShapeDescriptor&&)’
      *--__result = std::move(*--__last);
      ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algo.h:61:0,
                 from /usr/include/c++/7/functional:64,
                 from Detector.h:5,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_heap.h:128:5: warning: ‘void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Distance = long int; _Tp = ShapeDescriptor; _Compare = __gnu_cxx::__ops::_Iter_comp_val<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’ used but never defined
     __push_heap(_RandomAccessIterator __first,
     ^~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
                 from /usr/include/c++/7/memory:62,
                 from Detector.h:4,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/predefined_ops.h:169:7: warning: ‘__gnu_cxx::__ops::_Iter_comp_val<_Compare>::_Iter_comp_val(__gnu_cxx::__ops::_Iter_comp_iter<_Compare>&&) [with _Compare = Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)>]’ used but never defined
       _Iter_comp_val(_Iter_comp_iter<_Compare>&& __comp)
       ^~~~~~~~~~~~~~
In file included from /usr/include/c++/7/functional:64:0,
                 from Detector.h:5,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/stl_algo.h:1821:5: warning: ‘void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ShapeDescriptor*, std::vector<ShapeDescriptor> >; _Compare = __gnu_cxx::__ops::_Val_comp_iter<Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)> >]’ used but never defined
     __unguarded_linear_insert(_RandomAccessIterator __last,
     ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
                 from /usr/include/c++/7/memory:62,
                 from Detector.h:4,
                 from Detector.cpp:1:
/usr/include/c++/7/bits/predefined_ops.h:225:5: warning: ‘__gnu_cxx::__ops::_Val_comp_iter<_Compare> __gnu_cxx::__ops::__val_comp_iter(__gnu_cxx::__ops::_Iter_comp_iter<_Compare>) [with _Compare = Detector::evaluate()::<lambda(ShapeDescriptor, ShapeDescriptor)>]’ used but never defined
     __val_comp_iter(_Iter_comp_iter<_Compare> __comp)
     ^~~~~~~~~~~~~~~
makefile:11: recipe for target 'main' failed
make: *** [main] Error 1

Shape.cpp 是

#include "Shape.h"

ShapeDescriptor::ShapeDescriptor(){
    _type = UNDEFINED;
}

void ShapeDescriptor::copyFrom(const ShapeDescriptor &copy)
{
    _contour = copy._contour;    
    _type = copy._type;
    bounder = copy.bounder; 
}

ShapeDescriptor::ShapeDescriptor(std::vector<cv::Point> contour):
_contour{std::move(contour)}
{
   _type = UNDEFINED; 

   estimateShape();
   computePosition();
}

std::vector<cv::Point> ShapeDescriptor::getContour(){
    return _contour;
}

void ShapeDescriptor::estimateShape(){
    double peri = cv::arcLength( _contour, true);
    std::vector<cv::Point> approx;
    cv::approxPolyDP(_contour, approx, 0.04 * peri, true);
    int vertices = approx.size();
    bounder = cv::boundingRect(approx);

    if (getSize() < 25){
        return;
    }
    if (vertices == 3){
        _type = TRIANGLE;
    }
    else if (vertices == 4){
        double ratio = bounder.width/(double)bounder.height;

        if ((SQUARE_RATIO_L_LIMIT <= ratio) &&
                (ratio <= SQUARE_RATIO_U_LIMIT)) {
            _type = SQUARE;
        }else{
            _type = RECTANGLE;
        }
    }
    else if (vertices == 5){
        _type = POLYGON;
    }
    else{
        _type = CIRCLE;
    }

}


cv::Point ShapeDescriptor::ulCorner(){
    return cv::Point(bounder.x, bounder.y);
}

cv::Point ShapeDescriptor::urCorner(){
    return cv::Point(bounder.x + bounder.width,
            bounder.y);
}

cv::Point ShapeDescriptor::drCorner(){
    return cv::Point(bounder.x + bounder.width, 
            bounder.y + bounder.height);
}

cv::Point ShapeDescriptor::rEdge(){
    return cv::Point(bounder.x + bounder.width,
            bounder.y + bounder.height/2);
}

double ShapeDescriptor::getWidth(){
    return bounder.width;
}

double ShapeDescriptor::getHeight(){
    return bounder.height;
}

void ShapeDescriptor::computePosition(){
    _pos = cv::Point(bounder.x + bounder.width/2,
            bounder.y + bounder.height/2);
}
cv::Point ShapeDescriptor::getPosition(){
    return _pos;
}

Shape ShapeDescriptor::getType(){
    return _type;
}

double ShapeDescriptor::getSize(){
    return bounder.width * bounder.height;
}

void ShapeDescriptor::highlight(cv::Mat ref_img,
        cv::Scalar bc,
        cv::Scalar cc){
    std::vector<std::vector<cv::Point>> v;
    v.push_back(_contour);
    cv::drawContours(ref_img, v, 0, bc, 2);
    cv::circle(ref_img, _pos, 1, cc, -1);
}

bool ShapeDescriptor::isType(int s){
    return _type == s;
}

形状.h

#ifndef ShapeDescriptor_hpp
#define ShapeDescriptor_hpp

#include <tuple>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>

enum Shape {
    UNDEFINED = 0,
    CIRCLE = 1,
    SQUARE = 2,
    POLYGON = 3,
    TRIANGLE = 4,
    RECTANGLE = 5
};

class Color{
    public:
        static cv::Scalar GREEN;
        static cv::Scalar RED;
        static cv::Scalar BLUE; 
        static cv::Scalar WHITE; 
};

class ShapeDescriptor {

    private:
        const double SQUARE_RATIO_L_LIMIT = 0.70;
        const double SQUARE_RATIO_U_LIMIT = 1.5;  

        cv::Rect bounder;
        std::vector<cv::Point> _contour;
        cv::Point _pos;
        Shape _type;


        void computePosition();
        void estimateShape();

    public: 
        ShapeDescriptor();
        ShapeDescriptor(std::vector<cv::Point>);

        void copyFrom(const ShapeDescriptor&x);
        bool isType(int);

        void highlight(cv::Mat, cv::Scalar, cv::Scalar);

        cv::Point ulCorner();

        cv::Point drCorner();

        cv::Point urCorner();

        cv::Point rEdge();

        double getSize();
        std::vector<cv::Point> getContour(); 
        cv::Point getPosition();

        Shape getType();

        double getWidth();
        double getHeight(); 

};
#endif /* Shape_hpp */

最佳答案

第一条错误消息源于缺少 ShapeDescriptor 的移动赋值运算符:

error: use of deleted function ‘ShapeDescriptor& ShapeDescriptor::operator=(ShapeDescriptor&&)’

它还向您说明了为什么没有隐式移动赋值运算符:

Shape.h:31:7: note: ‘ShapeDescriptor& ShapeDescriptor::operator=(ShapeDescriptor&&)’ is implicitly deleted because the default definition would be ill-formed:
 class ShapeDescriptor {
       ^~~~~~~~~~~~~~~
Shape.h:31:7: error: non-static const member ‘const double ShapeDescriptor::SQUARE_RATIO_L_LIMIT’, can’t use default assignment operator
Shape.h:31:7: error: non-static const member ‘const double ShapeDescriptor::SQUARE_RATIO_U_LIMIT’, can’t use default assignment operator

这意味着该类具有 const 成员,因此编译器无法自行生成任何赋值运算符。请注意,为了对序列进行排序,编译器必须 swap元素,这要求类可以移动分配和移动构造 default .您还可以为 ShapeDescriptors 实现交换函数。

您的比较可能没问题,但根据 ShapeDescriptor 的大小,通过 const 引用获取要比较的参数可能更有效。

关于c++ - std::sort 用于 vector 在排序自定义对象时引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50232941/

相关文章:

c++ - std::map 发现在 C++ 中不起作用

java - Collections.sort() throws 比较方法违反了它的一般约定!异常(exception)

c++ - 如何计算快速排序期间发生的数组比较次数?

python - 使用simplecv在多个窗口中显示视频

c++ - 有没有更好的方法来为 C++ 编写这个?

c++ - 在凸包的弯曲点画圆

c++ - 递归删除链表c++

c++ - 为什么当我输入 12 位数字时,以下代码会崩溃?

Python OpenCV : Getting Stats out of Hough Circle Detection

android - 哪个是 HSV 皮肤检测器 openCV android 的颜色空间?