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

标签 c++ arrays string operator-keyword caesar-cipher

我正在做一个 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/

相关文章:

java - 数组初始化非法? ( java )

javascript - 从重复值中过滤具有唯一值的对象数组

java - Java 初学者 - 字符串格式 = "| %-"+maxW[j] +"s"; - 这个字符串有什么作用?

c++ - 为什么这个版本的 strcmp 比较慢?

c++ - 我可以创建一个 C++ "zap"函数来处理一个指针,如果它没有分配内存吗?

c++ - 具有特定参数的特定构造函数的类型声明

c++ - VC++2013 中嵌套可变参数模板结构的别名

c++ - 如何使用 libcurl 下载 utf-8 编码的网页,同时保留编码?

java - 数组显示错误值

python - 使用正则表达式分隔符拆分字符串,除非分隔符被转义