Подтвердить что ты не робот

Оператор "<<" не соответствует этим операндам

Не знаю, что происходит. Я посмотрел на другие сообщения, похожие на эту проблему, но до сих пор не удалось решить какое-либо решение. Здесь код с комментариями частей, дающих ошибки. В какой-то момент он говорит, что!= Не работает, а в остальной части кода он говорит, что < < не работает.

#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>

using namespace std;
//Hangman

int main()
{
    //setup
    vector<string> words;
    words.push_back("GUITAR");
    words.push_back("VIRGINIA");
    words.push_back("LAPTOP");
    words.push_back("WIFEY");
    words.push_back("IPHONE");

    srand(static_cast<unsigned int>(time(0)));   //randomly select a word
    random_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];
    const int MAX_WRONG = 8;                    //initialize wrong guesses
    int wrong = 0;
    string soFar(THE_WORD.size(), '-');         //initalize the word so far with dashes
    string used = " ";                          //initalize letters used


    cout << "Welcome to Hangman. Good luck!/n";

    //game loop
    //continues until a player guesses the word or gets too many wrong guesses
    while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) //ERROR on the "!="
    {
        cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
        cout << "\nYou've used the following letters:\n" << used << endl; //ERROR on "<<" between the string and used
        cout << "\nSo far, the word is:\n" << soFar << endl; //ERROR on "<<" between the string and soFar

    //recieve the player guess
        char guess;
        cout << "\n\nEnter your guess: ";
        cin >> guess;
        guess = toupper(guess); //makes the guess uppercase since the secret word is uppercase
        while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }

        used += guess;

        if (THE_WORD.find(guess) != string::npos)
        {
            cout << "That right! " << guess << " is in the word.\n";

            //updated soFar to include newly guessed letter
            for (int i=0; i < THE_WORD.length(); ++i)
            {
                if (THE_WORD[i] == guess)
                {
                    soFar[i] = guess;
                }
            }
        }
        else
        {
            cout << "Sorry, " << guess << " isn't in the word.\n";
            ++wrong;
        }
    }
    //shut down
    if (wrong == MAX_WRONG)
    {
        cout << "\nYou've been hanged!";
    }
    else
    {
        cout << "\nYou guessed it!";
    }

    cout << "\nThe word was " << THE_WORD << endl; //ERROR on "<<" between the string and THE_WORD


    int pause;
    cin >> pause; //this is here just so my compiler will stay open so I can see what going on
    return 0;
}
4b9b3361

Ответ 1

Если вы хотите надежно использовать std::string, вы должны #include <string>.

Ответ 2

Вы не включаете стандартный <string> заголовок.

Вам повезло, что некоторые из его соответствующих определений были случайно предоставлены другими стандартными заголовками, которые вы включили... но operator<< не было.

Ответ 3

Похоже, вы неправильно сравниваете строки. Чтобы сравнить строку с другой, используйте функцию std::string::compare.

Пример

     while ((wrong < MAX_WRONG) && (soFar.compare(THE_WORD) != 0))