我的代码抛出模板错误 (C++)

My code is throwing template errors (C++)

本文关键字:错误 C++ 代码 我的      更新时间:2023-10-16
几天

来我一直在反对这段代码。 它抛出一个:

template argument deduction/substitution failed:

第 119、124,129、162 行错误。(此处标有//错误 )

我仍然是编码方面的超级N00B,因此任何帮助将不胜感激。提前感谢!!

/*********************
Hangman v2
The classic game of hangman using multiple functions
(from chaper 5 C++ book)
**************************/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;

//GLOBALS!!
string THE_WORD;            //secret word player is tryign to guess
string soFar;               //word string ei: "G-ESS"
const int MAX_WRONG = 8;    // maximum number of incorrect guesses allowed
char guess;                 //character guessed by player
int wrong;                  //incorrect guesses

//Functions used (declarations)
string f_soFar();
void f_used();
int main();
vector<string> used_ch;        ///used characters

string getWord()
{
    vector<string> words;  // collection of possible words to guess
    words.push_back("GUESS");
    words.push_back("HANGMAN");
    words.push_back("DIFFICULT");
    words.push_back("ENTERPRISE");
    words.push_back("GALAXY");
    words.push_back("POMEGRANATE");
    words.push_back("CONTROLLER");
    srand(static_cast<unsigned int>(time(0)));  //Seed based on time
    random_shuffle(words.begin(), words.end()); //randomly picks from words vector
    string secretWord;    
    secretWord = words[0];            // word to guess
    THE_WORD = secretWord;
    cout<<"New Secret word has been chosen!!"<<endl;
    cout<<THE_WORD<<endl;
    return THE_WORD;
}
void check(char guess)       
{
    cout << "nnEnter your guess:nn->";
    cin >> guess;
    guess = toupper(guess); //make uppercase since secret word in uppercase
    while (find (used_ch.begin(), used_ch.end(), guess) != used_ch.end())
        {
            cout << "nYou've already guessed " << guess << endl;
            cout << "Enter another guess: ";
            cin >> guess;
            guess = toupper(guess);
        if (THE_WORD.find(guess) != string::npos)
            {
            cout << "That's right! " << guess << " is in the word.n";
            // update used_ch to include newly guessed letter
            for (unsigned int i = 0; i < THE_WORD.length(); ++i)
                {
                if (THE_WORD[i] == guess)
                    {
                    used_ch.push_back(&guess);
                    soFar[i] = guess;
                    cout<<"Guess again!"<<endl;
                    }
                }   
            }
            else
            {
                cout << "Sorry, " << &guess << " isn't in the word.n";
                ++wrong;
                cout<<"Try again!"<<endl;
            }
        }
}
void f_used()
{
    for(unsigned int i = 0; i < used_ch.size(); i++)
    {
        cout<<used_ch[i]<<", ";
    }
}
void replay()
{
    char choice;
    cout<<"Would you like to play again?nY/Nnn->";
    cin>>choice;
    if(choice == 'Y' || choice == 'y')
    {
        cout<<"OK!!!! Here we go...."<<endl;
        used_ch.clear();
        main();
    }
    else if(choice == 'N' || choice == 'n')
    {
        cout<<"OK, thanks for playing!!";
    }
    else //ERROR HERE
    {
        cout<<"Invalid Entry. please try again...";
        replay();
    }
}        //ERROR HERE
int main()
{
    // set-up
    getWord();    //ERROR HERE          //should get word from word()
    //cout<<"The word is "<<THE_WORD<<endl;  //TESTING PURPOSES!!

    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
    //STATUS
    cout << "nnYou have " << (MAX_WRONG - wrong);
    cout << " incorrect guesses left.n";
    cout << "nYou've used the following letters:n";
    f_used();
    cout << "nSo far, the word is:n" << soFar << endl;
    check(guess);
    }

    // shut down
    if (wrong == MAX_WRONG)
    {
        cout << "nYou've been hanged!";
        replay();
    }
    else
    {
        cout << "nYou guessed it!";
        cout << "nThe word was " << THE_WORD << endl;
        replay();
    }
}      //ERROR HERE

首先,让我们看一下完整的编译器错误:

c++     foo.cc   -o foo
In file included from foo.cc:7:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:846:22: error: invalid operands to binary expression
      ('std::__1::basic_string<char>' and 'int')
        if (*__first == __value_)
            ~~~~~~~~ ^  ~~~~~~~~
foo.cc:64:12: note: in instantiation of function template specialization 'std::__1::find<std::__1::__wrap_iter<std::__1::basic_string<char> *>, char>' requested here
    while (find (used_ch.begin(), used_ch.end(), guess) != used_ch.end())
           ^
1 error generated.

现在,让我们创建一个简化的测试用例:

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    std::vector<std::string> used_ch;
    char guess = 'c';
    find(used_ch.begin(), used_ch.end(), guess);
}

现在,我们看看问题是什么:

您正在尝试在字符串向量中查找char。但是,没有需要字符串和字符的operator==重载。