0

I have been beating my head against this code for a few days now. it throws a:

template argument deduction/substitution failed:

error on lines 119, 124,129, 162. (marked with //ERROR HERE )

i am still a super n00b at coding so any help would be appreciated. THANKS IN ADVANCE!!

/*********************
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
  • 2
    paste the full output from your compiler.
    – T.C.
    Commented Dec 3, 2014 at 23:34

1 Answer 1

2

First, let's look at the full compiler error:

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.

Now, let's create a reduced test case:

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

Now, we look at what the problem is:

You're attempting to find a char in a vector of strings. However, there is no overload of operator== that takes a string and a char.

2
  • this is why we want concepts
    – sp2danny
    Commented Dec 4, 2014 at 1:10
  • ah.... Changing the vector type to char fixed it. Its always the tiny details that kill everything...... THANKS ALL!!
    – Xolin_
    Commented Dec 4, 2014 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.