[经验总结]error: no match for ‘operator>’ (operand types are ‘const *’ and ‘const *’) { return _

索引

一、错误报告

二、报错代码

三、修正后的代码

四、原因和总结

一、错误报告

g++ 4.cpp -o 4.exe
In file included from /usr/include/c++/8/string:48,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_function.h: In instantiation of ‘constexpr bool std::greater<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Test]’:
/usr/include/c++/8/bits/predefined_ops.h:143:18:   required from ‘constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = Test*; _Iterator2 = Test*; _Compare = std::greater<Test>]’
/usr/include/c++/8/bits/stl_algo.h:1847:14:   required from ‘void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = Test*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<Test> >]’
/usr/include/c++/8/bits/stl_algo.h:1885:25:   required from ‘void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = Test*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<Test> >]’
/usr/include/c++/8/bits/stl_algo.h:1971:31:   required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = Test*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<Test> >]’
/usr/include/c++/8/bits/stl_algo.h:4866:18:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = Test*; _Compare = std::greater<Test>]’
4.cpp:15:32:   required from here
/usr/include/c++/8/bits/stl_function.h:376:20: error: no match for ‘operator>’ (operand types are ‘const Test’ and ‘const Test’)r
       { return __x > __y; }
                ~~~~^~~~~
4.cpp:7:7: note: candidate: ‘bool Test::operator>(const Test&)’ <near match>
  bool operator>(const Test &a){
       ^~~~~~~~
4.cpp:7:7: note:   passing ‘const Test*’ as ‘this’ argument discards qualifiers
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
                 from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:467:5: note: candidate: ‘template<class _T1, class _T2> constexpr bool std::operator>(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)’
     operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
     ^~~~~~~~
/usr/include/c++/8/bits/stl_pair.h:467:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/8/string:48,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_function.h:376:20: note:   ‘const Test’ is not derived from ‘const std::pair<_T1, _T2>’
       { return __x > __y; }
                ~~~~^~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:67,
                 from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_iterator.h:320:5: note: candidate: ‘template<class _Iterator> bool std::operator>(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)’
     operator>(const reverse_iterator<_Iterator>& __x,
     ^~~~~~~~
/usr/include/c++/8/bits/stl_iterator.h:320:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/8/string:48,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_function.h:376:20: note:   ‘const Test’ is not derived from ‘const std::reverse_iterator<_Iterator>’
       { return __x > __y; }
                ~~~~^~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:67,
                 from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_iterator.h:358:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> bool std::operator>(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)’
     operator>(const reverse_iterator<_IteratorL>& __x,
     ^~~~~~~~
/usr/include/c++/8/bits/stl_iterator.h:358:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/8/string:48,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_function.h:376:20: note:   ‘const Test’ is not derived from ‘const std::reverse_iterator<_Iterator>’
       { return __x > __y; }
                ~~~~^~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:67,
                 from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_iterator.h:1163:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> bool std::operator>(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)’
     operator>(const move_iterator<_IteratorL>& __x,
     ^~~~~~~~
/usr/include/c++/8/bits/stl_iterator.h:1163:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/8/string:48,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_function.h:376:20: note:   ‘const Test’ is not derived from ‘const std::move_iterator<_IteratorL>’
       { return __x > __y; }
                ~~~~^~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:67,
                 from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_iterator.h:1169:5: note: candidate: ‘template<class _Iterator> bool std::operator>(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)’
     operator>(const move_iterator<_Iterator>& __x,
     ^~~~~~~~
/usr/include/c++/8/bits/stl_iterator.h:1169:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/8/string:48,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_function.h:376:20: note:   ‘const Test’ is not derived from ‘const std::move_iterator<_IteratorL>’
       { return __x > __y; }
                ~~~~^~~~~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/basic_string.h:6171:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:6171:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/8/string:48,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_function.h:376:20: note:   ‘const Test’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
       { return __x > __y; }
                ~~~~^~~~~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/basic_string.h:6184:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)’
     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:6184:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/8/string:48,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_function.h:376:20: note:   ‘const Test’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
       { return __x > __y; }
                ~~~~^~~~~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/basic_string.h:6196:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
     operator>(const _CharT* __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:6196:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/8/string:48,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 4.cpp:1:
/usr/include/c++/8/bits/stl_function.h:376:20: note:   mismatched types ‘const _CharT*’ and ‘Test’
       { return __x > __y; }
                ~~~~^~~~~

二、报错代码

#include<iostream>
#include<algorithm>
using namespace std;
class Test{
public:
	int k;
	bool operator>(const Test &a){
		return this->k > a.k;
	}
};
Test A[100];
int main(){
	for(int i = 0;i < 100;i++)
		A[i].k = i;
	sort(A,A + 100,greater<Test>());
	return 0;
}

三、修正后的代码

#include<iostream>
#include<algorithm>
using namespace std;
class Test{
public:
	int k;
	bool operator>(const Test &a)const{
		return this->k > a.k;
	}
};
Test A[100];
int main(){
	for(int i = 0;i < 100;i++)
		A[i].k = i;
	sort(A,A + 100,greater<Test>());
	return 0;
}

四、原因和总结

原代码报错的地方是在greater<Test>()里.
greater的定义非常简单:

template <class T> struct greater {
  bool operator() (const T& x, const T& y) const {return x>y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

即简单第将x > y的值返回。

为什么这里会出错呢?
我们注意到greater类的小括号重载的参数为两个用const修饰的常对象。
因为是常对象,所以只能使用常成员,而我们原先的大于号重载是这样的:

bool operator>(const Test &a){
	return this->k > a.k;
}

即不是常成员,所以不能被成功调用。
使用const修饰之后,问题解决。


原创不易,感谢支持。

上一篇:项目批量修改版本号


下一篇:JUC练习13——forkJoin