无效的'islower'重载

Invalid overload of 'islower'

本文关键字:重载 islower 无效      更新时间:2023-10-16

我正在尝试在C 中开发战舰游戏,但我已经完成了。为此,我需要gameOver功能工作。当所有船只沉没时,我的游戏已经结束。因此,我正在尝试计算我的字符串状态(从船上)中有多少个小写字符。当一半的炭是小写的时,"船"被销毁了,我准备使用我的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);}

尝试更改算法调用以下方式

int lowercase = count_if (status.begin(), status.end(), ::islower); 
                                                        ^^^

允许编译器将标准C功能放置在全局名称空间中。

否则使用lambda表达式为

int lowercase = count_if (status.begin(), status.end(), 
                          []( char c ) return islower( c ); } );