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

时间:2014-12-03 23:21:09

标签: c++

我几天来一直在反对这段代码。它引发了一个:

template argument deduction/substitution failed:
第119,124,129,162行的

错误。(标有// ERROR HERE)

我仍然是编码的超级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 << "\n\nEnter your guess:\n\n->";
    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/N\n\n->";
    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 << "\n\nYou 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

1 个答案:

答案 0 :(得分:2)

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

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==没有带有字符串和字符的重载。