- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试用 g++ 编译一个在 Visual Studio 2012 中编译良好的程序。
我收到错误:error: no matching function for call to ‘ptr_fun(<unresolved overloaded function type>)’
据我所知,我已经包含了我需要的所有库和头文件。我错过了什么?
这是我的代码:
#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;
}
最佳答案
你应该在推断模板类型时给它一点帮助:
std::not1(std::ptr_fun<int, int>(isalnum)))
或者完全限定它:
std::not1(std::ptr_fun(::isalnum)))
由于语言环境功能,gcc 和 clang 似乎有多个重载决议候选者。
关于c++ - ‘ptr_fun(isalnum)’ 不能在 g++ 中编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26574286/
我问这个是对 this 的跟进问题。上一个问题几乎是三年前问的,所以我虽然问一个新的问题会更好。 我链接到的那个问题的症结在于 OP 试图运行以下代码行: find_if(s.begin(), s.e
您好! 这次让我们减少过多的介绍,直奔主题。 我在 C++ 中使用 isalnum 方法时遇到问题。 代码: int playAgainst = 0; do { cout > playAgai
char dict[N][M]; for(i = 0; i < N; i++) { do { printf("enter word at position: %d\n"
我正在尝试转换一些代码,这些代码旨在从命令行参数中删除除“_”之外的所有非数字字符,除了我试图让代码接受来自常规字符串,我尝试将代码转换为接受字符串,但我一直收到此错误 words.c:9: warn
我正在尝试确定我的字符串中的每个字符是否都是字母数字。我的编译器没有 isalnum 函数。 我的函数在下面,my_struct 有一个大小为 6 的字符数组 (uint8 bom_pn[6]) ..
对于赋值,我使用 std::isalnum 来确定输入是字母还是数字。任务的重点是创建一个“字典”。它在小段落上效果很好,但在文本页面上却很糟糕。这是我正在使用的代码片段。 custom::Stri
我有一个非常简单的程序,我在其中使用 isalnum 函数来检查字符串是否包含字母数字字符。代码是: #include "stdafx.h" #include #include #include
我尝试检查字符串的字符是否为 alnum,然后检查字符串是否仅包含 alnum 字符以打印字符串。当我运行该程序时,什么也没有发生。我有另一个程序,我用它从输入中读取我想要的文本,然后用 FIFO 发
我只有isalnum_t函数 ctype ;我期望功能isalnum那么发生了什么?我包括ctype这个函数是我需要检查字母和数字被声明为 isalnum_t(int, _locale_t) 。网上查
我正在尝试用 g++ 编译一个在 Visual Studio 2012 中编译良好的程序。 我收到错误:error: no matching function for call to ‘ptr_fun
我正在使用 g++ 4.7。 我想做的是这个, find_if(s.begin(), s.end(), isalnum); 其中 isalnum 在 cctype 中定义,s 是一个字符串。 logm
我正在用法语编程,因此,我需要使用重音字符。我可以使用输出它们 #include 和 setlocale(LC_ALL, "") , 但是当我读重音字符时似乎有问题。这是我用来说明问题的简单示例: #
我要拆分 "Use a regexp that matches any combination\nthat satisfies Python's\n\nisalphanumeric() or issp
我写了一个扩展 isalnum 的函数来识别 UTF-8 编码的变音符号。 是否有更优雅的方法来解决这个问题? 代码如下: bool isalnumlaut(const char character)
我用谷歌搜索了我的问题,也许我没有使用正确的搜索词,因为我没有找到解决我问题的方法。 我想做的是使用 push 和 pop 方法和模板做一个后缀计算器表达式。 教授的话 Input The input
我是一名优秀的程序员,十分优秀!