gpt4 book ai didi

c++ - int 在 operator== 中显示为 std::basic_ostream

转载 作者:行者123 更新时间:2023-11-30 05:35:06 26 4
gpt4 key购买 nike

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

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

代码:

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);
^

提前致谢!

最佳答案

不,“line”变量被视为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 ,以便您可以通过重复 >> 来继续将内容附加到流中运营商。

这只是运算符优先级的问题:>>运算符的优先级高于 ==运算符(operator)。因此,编译器首先评估 cout << line , 类型为 std::basic_ostream ,然后它会尝试评估加上 == (key - 1) .哪个不计算。

因此,您需要一些括号来告诉编译器您想要 ==line 之间应用的运算符和 (key - 1) , 以及输出到流的结果。

所以,你需要的是cout << (line == key - 1) .

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

关于c++ - int 在 operator== 中显示为 std::basic_ostream<char>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33980066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com