无效的'重载;岛民'; 我正在尝试开发C++的战舰游戏,我差不多完成了。为此,我需要我的gameOver函数工作。当所有的船都沉没时,我的游戏结束了。因此,我试图计算我的字符串状态中有多少小写字符(来自Ship)。当一半字符是小写时,“飞船”被销毁,我准备使用我的gameOver函数
但不知何故,如果我的无效的'重载;岛民'; 我正在尝试开发C++的战舰游戏,我差不多完成了。为此,我需要我的gameOver函数工作。当所有的船都沉没时,我的游戏结束了。因此,我试图计算我的字符串状态中有多少小写字符(来自Ship)。当一半字符是小写时,“飞船”被销毁,我准备使用我的gameOver函数,c++,string,C++,String,但不知何故,如果我的不起作用,我不知道为什么 给你: #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.le
不起作用,我不知道为什么
给你:
#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;
}
#包括
bool Ship::isDestroyed()常量{
//这将计算满足islower要求的字符数:
int lowercase=count_if(status.begin()、status.end()、islower);
return(小写不幸的是,标准库有多个重载islower
(一个来自C库的函数和一个来自本地化库的函数模板),因此除非调用它,否则不能简单地命名函数
您可以将其转换为正确的函数类型:
static_cast<int (*)(int)>(islower)
或者用羔羊皮包起来
[](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 ); } );
“不工作”是什么意思?它是否返回0、意外值、预期值的一半、最大值等等?ships[i]。isDestroyed()==false
更清楚地写为!ships[i]。isDestroyed()
@KeithThompson耶!那isDestroyed()中的返回值如何
?在isDestroyed()
中的return
语句不应该是return(小写>=(status.length/2))?true:false;
(=
,而不是@RSahu);它应该是return lowercase>=status.length/2;
(wink)它正在工作。非常感谢。gameOver函数虽然不起作用,但我想我需要一个新的主题。@可能你指的是返回状态。length/2=(status.length/2))?true:false;即小写字母大于字符串的一半。
int lowercase = count_if (status.begin(), status.end(),
[]( char c ) return islower( c ); } );