gpt4 book ai didi

c++ - 'islower' 的无效重载

转载 作者:太空狗 更新时间:2023-10-29 19:43:22 25 4
gpt4 key购买 nike

我正在尝试用 C++ 开发 BattleShip 游戏,我快完成了。为此,我需要我的 gameOver 函数正常工作。当所有的船都沉没时,我的游戏就结束了。因此,我试图计算我的字符串状态(来自 Ship)中有多少个小写字符。当一半字符为小写时,“船”被摧毁,我准备好使用我的 gameOver 函数。

但不知何故,我的 count_if 不工作,我不知道为什么。

给你:

#include <algorithm>

bool Ship::isDestroyed() const{
//This counts those chars that satisfy islower:
int lowercase = count_if (status.begin(), status.end(), islower);
return ( lowercase <= (status.length/2) ) ? true : false;
}

bool Board::gameOver() {
bool is_the_game_over = true;
for(int i = 0 ; i < ships.size() ; i++){
if( ships[i].isDestroyed() == false ) {
//There is at least one ship that is not destroyed.
is_the_game_over = false ;
break;
}
}
return is_the_game_over;
}

我做错了什么?

最佳答案

不幸的是,标准库有不止一个islower重载(一个来自C库的函数,一个来自本地化库的函数模板),所以你不能简单地命名函数,除非你在调用它。

您可以将其转换为正确的函数类型:

static_cast<int (*)(int)>(islower)

或者希望您的实现将 C 库转储到全局命名空间以及 std 中:

::islower      // not guaranteed to work

或者用lambda包起来

[](int c){return islower(c);}

关于c++ - 'islower' 的无效重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30353012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com