无效的'islower'重载
Invalid overload of 'islower'
我正在尝试在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 ); } );
相关文章:
- 继承函数的重载解析
- 你能重载对象变量名本身返回的内容吗
- 从父命名空间重载类型
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 重载操作程序时出错>>用于类中的字符串 memebr
- 一个关于在C++中重载布尔运算符的问题
- 不同翻译单元中不可重载的非内联函数定义
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 将重载的成员函数传递给函数模板
- c++:可变模板和函数重载
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 重载==不适用于二进制树
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 重载运算符new[]的行为取决于析构函数
- 正在尝试重载二进制搜索树分配运算符
- 重载Singly Linked List中的赋值运算符
- 无效的'islower'重载