gpt4 book ai didi

c++ - 函数中不匹配 'operator=='

转载 作者:行者123 更新时间:2023-11-27 22:59:15 25 4
gpt4 key购买 nike

我正在做一个 caesar decipher 项目,在 CaesarDecipher 函数中,我在编译时不断收到这个错误:

error: no match for ‘operator==’ in ‘textInit.std::basic_string<_CharT, _Traits, >_Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits, _Alloc >= std::allocator](((long unsigned int)i)) == alphabet[j]’

这是该函数的代码:

string CipherMessage::CaesarDecipher(string key)
{
int keyValue;
int charValue;
string textInit = m_text;
string textFinal;
// Initializes an array containing the alphabet. A=index 0, B=index 1, etc
string alphabet[26] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","\
O","P","Q","R","S","T","U","V","W","X","Y","Z"};

for (int i=0; i<=25; i++){
if (alphabet[i] == key)
keyValue = i;
}
for (int i=0; i<=textInit.length(); i++){
for (int j=0; j<=25; j++){
if (textInit[i] == alphabet[j]) // Error occurs here
charValue = j;
}
charValue = (charValue+keyValue)%26;
for (int j=0; j<=25; j++){
if (charValue == j)
textFinal += alphabet[j];
}
}
cout << "Final " << textFinal << endl;

return textFinal;
}

有人能帮忙吗?

最佳答案

你可能打算像这样声明alphabet

string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

删除此错误消息。

正如在评论中提到的,您改为声明了一个包含 26 个完整 string 的数组,但是从 textInit[i] 进行索引实际上会返回一个 char 类型(不能反过来合理地与 std::string 进行比较)。

关于c++ - 函数中不匹配 'operator==',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29334877/

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