C++错误:从'char'到'const char*'的转换无效

Laz*_*h13 6 c++

我是C++的新手,我创建了这个函数:

bool guessWord(string compWord)
{
    cout << "Guess a letter: ";
    string userLetter;
    cin >> userLetter;
    for (unsigned int x = 0; x < compWord.length(); x++)
    {
        string compLetter = compWord[x];
        if (compLetter == userLetter)
        {
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

但它回归到以下error: invalid conversion from 'char' to 'const char*' [-fpermissive].任何人都可以帮我理解这意味着什么?

For*_*veR 4

string compLetter = compWord[x];
Run Code Online (Sandbox Code Playgroud)

compWord[x]getschar并且您试图将其分配给string,这是错误的。但是,您的代码应该类似于

bool guessWord(string compWord)
{
    cout << "Guess a letter: ";
    char userLetter;
    cin >> userLetter;
    for (unsigned int x = 0; x < compWord.length(); x++)
    {
        char compLetter = compWord[x];
        if (compLetter == userLetter)
        {
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)