c++ - 为什么在比较字符串时出现错误 "no match for ' operator= =' "?

标签 c++ string

<分区>

对于我将一个字符串与另一个字符串进行比较的每一行,我都会不断收到错误:

“operator==”不匹配

代码:

#include <iostream>
#include <conio.h>
#include <string>
#include <curses.h>
using namespace std;

int word_number, state, i, x, n;
bool correct[25], playing = true, set_complete, valid, match;
string word, word_list[25], head, upper, body, lower, blanks, input, guessed, alphabet = "abcdefghijklmnopqrstuvwxyz";
size_t found;

void play(), initialize(), print(), validate(), progress();

int main()
{
    initscr();
    while (playing)
    {
        play();
        printw("Would you like to continue playing?");
        input = getch();
        if (input == "n"||input == "N")
        {
            playing = false;
        }
    }
    endwin();
}

void initialize()
{
    if (word_number != 0)
    {
        word_number++;
    }

    if (word_number == 25)
    {
        set_complete = true;
    }
    state = 0;
    head = "";
    upper = "";
    body = "";
    lower = "";
    blanks = "";
    guessed = "";
    word  =  word_list[word_number];
    for (i = 0; i<strlen(word.c_str()); i++)
    {
        blanks += "_";
    }
}

void play()
{
    initialize();
    while(!set_complete)
    {
        start:
        input = getch();
        validate();

    }
}

void validate()
{

    for (i = 0, valid = false; i <= 25; i++)
    {
        if (input == alphabet[i])
        {
            valid = true;
        }
    }

    if (!valid)
    {
        goto start;
    }

    for (i = 0, match = false; i<strlen(guessed.c_str()); i++)
    {
        if (guessed[i] == input)
        {
            match = true;
        }
    }

    if (!match)
    {
        guessed += input;
    }

    for (i = 0, match = false; i<strlen(word.c_str()); i++)
    {
        if (input == word[i])
        {
            blanks[i] = input;
            match = true;
        }
    }

    if (!match)
    {
        state++;
    }
    else
    {
        x++;
    }

    if (x == strlen(word.c_str()))
    {
        correct[word_number] = 1;
        initialize();
    }
}

void print()
{
    switch (state)
    {
        case 1:
        head = "(Q)";
        break;
        case 2:
        upper = " |";
        break;
        case 3:
        upper = "\|";
        break;
        case 4:
        upper = "\|/";
        break;
        case 5:
        body = "|";
        break;
        case 6:
        lower = "/ ";
        break;
        case 7:
        lower = "/ \\";
        break;
        default:
        break;
    }

    printw("   ______\n");
    printw("  /      \\\n");
    printw("  |     %s\n", head.c_str());
    printw("  |    %s\n", upper.c_str());
    printw("  |     %s\n", body.c_str());
    printw("  |    %s\n", lower.c_str());
    printw("__|__\n");
    if (!valid)
    {
        printw("Only lowercase letters are allowed!\n");
        valid = true;
    }
    printw("%s\n", guessed.c_str());
    for (i = 0; i<strlen(word.c_str()); i++)
    {
        printw("%s ", blanks[i].c_str());
    }
    refresh();
}

void progress()
{
    for (i = 0; i<25; i++)
    {
        if (correct[i] =  = 1)
        {
            printw("%s -- Correct!\n", word_list[word_number].c_str());
        }
        else
        {
            printw("%s -- Incorrect.\n", word_list[word_number].c_str());
        }
    }
}

Here is my source code in its entirety

错误出现在第72、85、98行

编辑: 按照建议删除标签后,我的问题的解决方案是简单地将 input to a char 的比较实例替换为 input[0] to a char。

最佳答案

if (input == alphabet[i]) 左边有一个 std::string,右边有一个 char,所以比较显然没有定义。

您可能想遍历 input 中的所有字符并比较 input[n](n 是当前索引)是否包含在 alphabet< 中。您可以使用 alphabet.find 来更快地完成此操作(或者您可以利用字符的 ASCII 表示形式)。

关于c++ - 为什么在比较字符串时出现错误 "no match for ' operator= =' "?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5921784/

相关文章:

c++ - 如何从一组已定义的描述符中动态构建新的 protobuf?

c++ - Negamax 实现似乎不适用于井字游戏

java - 在 Java 中将字符串解析为 int 的快速方法

c - 过滤字符串中连续重复的字符

c++ - 如何将 char 字符串转换为 wchar_t 字符串?

java - 在Java中将资源文本文件读取为字符串

c++ - 为什么VC2008认为这个类是抽象的?

c++ - 模板函数中依赖于类型的常量

c++ - Reference_wrapper<string> 不会在 cout 中打印,但 reference_wrapper<int> 会吗?

谁能解释一下这个功能是如何工作的?