没有匹配这些操作数的 "<<" 运算符。

26

我不知道发生了什么事情。我查看了其他与此问题类似的帖子,但迄今没有解决方案。以下是带有注释的代码,这些部分会出现错误。其中一个地方说!=不起作用,在其余的代码中则表示<<不起作用。

#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's 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's 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's going on
    return 0;
}
3个回答

66
如果您想可靠地使用std::string,则必须#include <string>

10

您没有包含标准的<string>头文件。

您运气好,因为您包含的其他标准头文件中意义相关的一些定义是意外可用的......但operator<<不是其中之一。


-1

看起来你正在错误地比较字符串。要将一个字符串与另一个字符串进行比较,请使用{{link1:std::string :: compare }}函数。

示例

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

4
不需要。对于std::string,有很多重载的!=可以做正确的事情,并且这是在C++中惯用的方式。 - Alan Stokes
1
哦,谢谢。老实说,我不知道。因为我从C语言开始学习编程,已经有几年了。我查看了参考文档,但从未注意到重载这个概念;不用说,我有点羞愧。 - Brett
1
没有任何羞耻感。在C++中,重载!=和==只是处理这个问题的正常方式;如果一个类以其他方式支持相等性,那将是奇怪的。 - Alan Stokes
3
那条评论是“完全没用的”,Alan 的评分和评论已经足够了,没有必要说话恶劣。 - Brett

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接