简体   繁体   English

'ptr_fun(isalnum)'不能用g ++编译?

[英]‘ptr_fun(isalnum)’ won't compile in g++?

I am attempting to compile a program in g++ which compiled fine in Visual Studio 2012. 我正在尝试用g ++编译一个程序,该程序在Visual Studio 2012中编译得很好。

I am receiving the error : error: no matching function for call to 'ptr_fun(<unresolved overloaded function type>)' 我收到错误: error: no matching function for call to 'ptr_fun(<unresolved overloaded function type>)'

To the best of my knowledge, I have included all the library and header files that I need. 据我所知,我已经包含了我需要的所有库和头文件。 What am I missing? 我错过了什么?

Here is my code: 这是我的代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <functional>
#include <ctype.h>
#include <stdio.h>

using namespace std;

int main() {

    string secretWord; //to store our secret word
    string secretWordClean = ""; //given an initial value so that we can add the alpha-only characters from secretWord
    string guessedLetters; //to be loaded with _ characters equal to length of secretWord
    string incorrectlyGuessedChars = ""; //for storing our incorrectly guessed characters to be show to the user
    char individualCharGuess; //to temporarily store the individual char guess of the user
    char playAgain; //will be set to Y to play again, and tested in an if statment
    size_t countOfLetters = 0; //begine count at 0
    size_t guessesRemaining; //to store the remaining guesses of the user, will be decrimented each iteration
    int guessedUsed; //to calulate how many guesses the player used when game is complete

begin_game://label which we can use to bring us back to the start of the do-while loop at any time

    cout << "Please enter a secret word: ";

    std::cin >> std::ws;
    getline(std::cin, secretWord); 
    secretWord.erase(std::remove_if(secretWord.begin(), secretWord.end(), std::not1(std::ptr_fun(isalnum))), secretWord.end());

    for(int i = 0; i < secretWord.length(); i++){
        if (isalpha(secretWord[i])){ 
            secretWordClean += secretWord[i];
        }
    }

    secretWord = secretWordClean; //assign all alpha secret word string back to original variable for better readability
    guessesRemaining = secretWord.length() * 2;

    for(int i = 0; i < secretWord.length(); i++){
        guessedLetters += "_"; //fills guessedLetters with blanks equal to the length of the secretWord
    }

    do{//start of the guessing portion of game

        cout << "Please guess a letter, you have " << guessesRemaining << " guesses remaining!" << endl;
        cin >> individualCharGuess;

        for(int i = 0; i < secretWord.length(); i++){ //every complete iteration of this for loop = one single guess
            if(secretWord[i] == individualCharGuess){
                guessedLetters[i] = individualCharGuess; //will replace the spaces with the correct character, if guessed
                countOfLetters++; //if any letter is guessed correctly, this indicator will be incrimented above 0
                continue;
            }

            if(secretWord.find(individualCharGuess) == string::npos){
                if(incorrectlyGuessedChars.find(individualCharGuess) == string::npos){
                    incorrectlyGuessedChars += individualCharGuess;
                }
            }
        }

        if(secretWord.compare(guessedLetters) == 0){
            cout << "You win! The word was: " << secretWord << endl;
            guessedUsed = ((secretWord.length() * 2) - guessesRemaining) + 1 ;
            cout << "You used " << guessedUsed << " guesses." << endl; 
            cout << "Play again? Enter Y for Yes, or anything else to exit: ";
            cin >> playAgain;
            if(playAgain != 'Y'){
                break; //exit the loop if user guesses all the letters and doesn't want to play again
            }
            else {
                incorrectlyGuessedChars = "";
                secretWordClean = "";
                guessedLetters = "";
                //continue;
                goto begin_game;
            }
        }

        guessesRemaining--; //we decriment our total guesses remaining if the user does not win the game or run out of guesses

        if(countOfLetters > 0){
            cout << "You have correctly guessed a letter!" << endl;
            cout << "Here are the letters you have guessed correctly so far: ";
            cout << guessedLetters << endl;
            cout << "Here are the letters you have guessed incorrectly so far: ";
            cout << incorrectlyGuessedChars << endl;
            countOfLetters = 0; //reset the counter to prepare for next iteration of do-while loop
        }
        else if (guessesRemaining <= 0) {
            cout << "You have run out of guesses!" << endl;
            cout << "Here are the letters that you guessed correctly: ";
            cout << guessedLetters << endl;
            cout << "Here are the letters you guessed incorrectly: ";
            cout << incorrectlyGuessedChars << endl;
            cout << "The secret word was: " << secretWord << endl;
            cout << "Play again? Enter Y for Yes, or anything else to exit: ";
            cin >> playAgain;
            if(playAgain != 'Y'){
                break; //exit the loop if user guesses all the letters and doesn't want to play again
            }
            else {
                secretWordClean = "";
                incorrectlyGuessedChars = "";
                guessedLetters = "";
                //continue;
                goto begin_game;
            }
        }
        else {
            cout << "You guessed wrong! Keep trying, " << guessesRemaining << " guesses to go!" << endl;
            cout << "Here are the letters you have guessed correctly so far: ";
            cout << guessedLetters << endl;
            cout << "Here are the letters you have guessed incorrectly so far: ";
            cout << incorrectlyGuessedChars << endl;
        }

    }while (secretWord.compare(guessedLetters) != 0 || guessesRemaining != 0); //use to repeat the request for a single char guess

    return 0;
}

You should give it a little help in deducing the template types: 在推导模板类型时,您应该给它一些帮助:

std::not1(std::ptr_fun<int, int>(isalnum)))

or by fully qualifying it: 或完全符合条件:

std::not1(std::ptr_fun(::isalnum)))

it seems that gcc and clang have multiple overload resolution candidates due to the locale functions. 由于语言环境功能,gcc和clang似乎有多个重载决策候选者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM