无操作符“<<匹配这些操作数 [英] no operator "<<" matches these operands

查看:130
本文介绍了无操作符“<<匹配这些操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道发生了什么。我看了其他类似这个问题的帖子,但没有解决方案到目前为止。这里是代码与部分给出错误的评论。有一点,它说,!=不工作,在其余的代码,它说,<<不工作。

No idea what's going on. I looked at other posts that are similar to this issue but no solution helped so far. Here's the code with comments by the parts giving errors. At one point it says that != doesn't work and in the rest of the code it's saying that << isn't working.

#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;
}


推荐答案

code> std :: string 可靠,您必须 #include< string>

If you want to use std::string reliably, you must #include <string>.

这篇关于无操作符“&lt;&lt;匹配这些操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆