GCC:为什么一行代码中的错误(字符串和NULL的比较)会导致一长串错误消息

GCC: Why does an error in one line of code (comparison of string and NULL) cause a long list of error messages?

本文关键字:错误 比较 消息 NULL 字符串 一行 为什么 代码 GCC      更新时间:2023-10-16

我一直很好奇为什么一个错误会导致编译器生成一长串错误消息。以下示例是GCC 4.8.1:下main.cpp第100行的vector<string>NULL元素之间错误比较的结果

> g++ -g3 -std=c++11 main.cpp functions.cpp
main.cpp: In function ‘int main()’:
main.cpp:100:24: error: no match for ‘operator==’ (operand types are ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ and ‘long int’)
             if(args[1] == NULL) {
                        ^
main.cpp:100:24: note: candidates are:
In file included from /usr/include/c++/4.8/iosfwd:40:0,
                 from /usr/include/c++/4.8/ios:38,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/postypes.h:216:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
     operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
     ^
/usr/include/c++/4.8/bits/postypes.h:216:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::fpos<_StateT>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/stl_pair.h:214:5: note: 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++/4.8/bits/stl_pair.h:214:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::pair<_T1, _T2>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:291:5: note: 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++/4.8/bits/stl_iterator.h:291:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::reverse_iterator<_Iterator>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:341:5: note: 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++/4.8/bits/stl_iterator.h:341:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::reverse_iterator<_Iterator>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:1031:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)
     operator==(const move_iterator<_IteratorL>& __x,
     ^
/usr/include/c++/4.8/bits/stl_iterator.h:1031:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::move_iterator<_Iterator>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:1037:5: note: template<class _Iterator> bool std::operator==(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
     operator==(const move_iterator<_Iterator>& __x,
     ^
/usr/include/c++/4.8/bits/stl_iterator.h:1037:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::move_iterator<_Iterator>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/string:41:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/allocator.h:128:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_T2>&)
     operator==(const allocator<_T1>&, const allocator<_T2>&)
     ^
/usr/include/c++/4.8/bits/allocator.h:128:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::allocator<_CharT>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/string:41:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/allocator.h:133:5: note: template<class _Tp> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_CharT>&)
     operator==(const allocator<_Tp>&, const allocator<_Tp>&)
     ^
/usr/include/c++/4.8/bits/allocator.h:133:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::allocator<_CharT>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/basic_string.h:2486:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2486:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘long int’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/basic_string.h:2493:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
     operator==(const basic_string<_CharT>& __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2493:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   mismatched types ‘const std::basic_string<_CharT>’ and ‘long int’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/basic_string.h:2507:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator==(const _CharT* __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2507:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   mismatched types ‘const _CharT*’ and ‘std::basic_string<char>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/basic_string.h:2519:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2519:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   mismatched types ‘const _CharT*’ and ‘long int’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/bits/locale_facets.h:48:0,
                 from /usr/include/c++/4.8/bits/basic_ios.h:37,
                 from /usr/include/c++/4.8/ios:44,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/streambuf_iterator.h:204:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
     operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
     ^
/usr/include/c++/4.8/bits/streambuf_iterator.h:204:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::istreambuf_iterator<_CharT, _Traits>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/vector:64:0,
                 from main.h:16,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/stl_vector.h:1403:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
     operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
     ^
/usr/include/c++/4.8/bits/stl_vector.h:1403:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::vector<_Tp, _Alloc>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/x86_64-suse-linux/bits/c++allocator.h:33:0,
                 from /usr/include/c++/4.8/bits/allocator.h:46,
                 from /usr/include/c++/4.8/string:41,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/ext/new_allocator.h:139:5: note: template<class _Tp> bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&)
     operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
     ^
/usr/include/c++/4.8/ext/new_allocator.h:139:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const __gnu_cxx::new_allocator<_Tp>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:811:5: note: template<class _Iterator, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
     operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
     ^
/usr/include/c++/4.8/bits/stl_iterator.h:811:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’
             if(args[1] == NULL) {
                           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.h:13,
                 from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:805:5: note: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
     operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
     ^
/usr/include/c++/4.8/bits/stl_iterator.h:805:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
                 from main.h:20,
                 from main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’
             if(args[1] == NULL) {
                           ^

这会使人们很难理解这个问题。这种行为的原因是什么?

如果您有一个形式为vec == NULL的表达式,编译器会应用ADL来查找要调用的适当operator==。如果它找不到一个,它会输出在所有考虑的命名空间中找到的所有operator==候选者,以及它们不适合此处的原因。

其中一个可能看起来像这样:

In file included from [..] main.cpp:12:
[..]:216:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
     operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
     ^
[..]:216:5: note:   template argument deduction/substitution failed:
In file included from [..] main.cpp:12:
main.cpp:100:27: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::fpos<_StateT>’
             if(args[1] == NULL) {
                           ^

我们无法将第一个参数与第一个参数相匹配,因为std::string不是从任何fpos-specialization派生的(std::string也没有任何合适的转换运算符,但这里没有提到)。如果您定义了一个无法找到/调用的operator==,您不想从编译器视图中知道原因吗?(尤其是如果你自己没有线索的话)

这有时非常有用。如果不是,忽略它。在大多数情况下,C++程序员会立即看到并理解错误消息的原因(及其enourmos note部分)。

在这种情况下(您收到了大量与模板相关的消息),这可能会有所帮助
无论您是使用make还是直接使用编译器,请尝试将stderr管道传输到我的示例中的文件

make 2>error.log

使用任意编辑器打开error.log文件。搜索您编写的源代码的文件名。应该只有一两行你感兴趣。例如:

mysourcefile.cpp:51:20:   required from here

这就是问题发生的地方;在这里看得更仔细。