int在operator ==

时间:2015-11-29 06:28:09

标签: c++ type-conversion

我正在开发一个可以加密和解密Rails-Cipher的程序。在这样做的过程中,我遇到了一个错误,其中以下代码永远不会到达“inc!= inc”行。

在查看错误消息后,我发现这是因为“line”变量被视为std :: basic_ostream。我一直在环顾四周,无法弄清楚原因。我声明它是一个int。 Key是RailsCipher类中的int。我把我正在努力的代码以及我从中得到的错误。

CODE:

string RailsCipher::encrypt()
{
    string array[key];
    bool inc = true;
    int line = 0;
    for (int p = 0; p < data.length(); ++p, inc ? ++line : --line)
    {
        array[line].push_back(data[p]);
        cout << line == 0;
        cout << line == (key - 1);
        if (line == 0 || line == (key - 1))
            inc != inc;
    }
    string encrypted;
    for (int i = 0; i < key; i++)
        encrypted += array[i];
    return encrypted;
}

错误:

rail.cpp: In member function ‘std::string RailsCipher::encrypt()’:
rail.cpp:27:22: error: no match for ‘operator==’ (operand types are ‘std::basic_ostream<char>’ and ‘int’)
         cout << line == (key - 1);
                      ^
rail.cpp:27:22: 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 rail.cpp:1:
/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:
rail.cpp:27:33: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::fpos<_StateT>’
         cout << line == (key - 1);
                                 ^
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 rail.cpp:1:
/usr/include/c++/4.8/bits/stl_pair.h:214:5: note: template<class _T1, class _T2> 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:
rail.cpp:27:33: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::pair<_T1, _T2>’
         cout << line == (key - 1);
                                 ^
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 rail.cpp:1:
/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:
rail.cpp:27:33: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
         cout << line == (key - 1);
                                 ^
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 rail.cpp:1:
/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:
rail.cpp:27:33: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
         cout << line == (key - 1);
                                 ^
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 rail.cpp:1:
/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:
rail.cpp:27:33: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::allocator<_CharT>’
         cout << line == (key - 1);
                                 ^
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 rail.cpp:1:
/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:
rail.cpp:27:33: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::allocator<_CharT>’
         cout << line == (key - 1);
                                 ^
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 rail.cpp:1:
/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:
rail.cpp:27:33: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
         cout << line == (key - 1);
                                 ^
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 rail.cpp:1:
/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:
rail.cpp:27:33: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::basic_string<_CharT>’
         cout << line == (key - 1);
                                 ^
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 rail.cpp:1:
/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:
rail.cpp:27:33: note:   mismatched types ‘const _CharT*’ and ‘std::basic_ostream<char>’
         cout << line == (key - 1);
                                 ^
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 rail.cpp:1:
/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:
rail.cpp:27:33: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
         cout << line == (key - 1);
                                 ^
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 rail.cpp:1:
/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:
rail.cpp:27:33: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::istreambuf_iterator<_CharT, _Traits>’
         cout << line == (key - 1);
                                 ^

提前致谢!

1 个答案:

答案 0 :(得分:0)

不,“行”变量被视为std::basic_ostream

错误no match for ‘operator==’ (operand types are ‘std::basic_ostream<char>’ and ‘int’)表示编译器认为您尝试在==cout << line之间应用(key - 1)运算符。如:(cout << line) == (key - 1)

您看,cout的类型为std::basic_ostream,而cout << line的结果也属于std::basic_ostream类型,以便您可以将内容添加到通过重复>>运算符来流式传输。

这只是运算符优先级的问题:>>运算符的优先级高于==运算符。因此,编译器首先评估类型为cout << line的{​​{1}},然后尝试评估加std::basic_ostream。哪个不计算。

因此,您需要一些括号来告诉编译器您希望在== (key - 1)==之间应用line运算符,并将其结果输出到流

所以,你需要的是(key - 1)

(我认为原始cout << (line == key - 1)中的括号是不必要的。)