c++ - 布尔函数作为类成员条件不返回值
问题描述
#include <iostream>
#include <vector>
using namespace std;
enum type{
noboard,
peg,
empty
};
class board{
public:
//create getter
vector<vector<int>>getVector() const;
//create setter
bool control_vect();
board(vector<vector<int>> pvect);
//display() function that prints elements of the vect data member
void display()
{
for(const vector<int> &elem: vect)
{
for(int intElem: elem)
{
if(intElem==1){
cout<<'P';
}
else if(intElem==0){
cout<<' ';
}
else if(intElem==2){
cout<<'.';
}
}
cout<<endl;
}
}
private:
vector<vector<int>> vect;
};
vector<vector<int>> board :: getVector() const
{
return vect;
}
board :: board(vector<vector<int>> pvect)
{
vect = pvect;
}
bool board :: control_vect(){
long unsigned int i,j;
bool controller=false;
cout<<vect[0][0];
for (i = 0; i < vect.size(); i++){
for ( j = 0; j < vect[i].size()-2; j++){
if(vect[i][j]==1 && vect[i][j+1]==1 && vect[i][j+2]==0){
controller=true;
break;
}
}
}
return controller;
}
int main()
{
//create a board instance
// board myBoard;
vector<vector<int>> pvect{
{peg,peg,peg,peg,peg},
{noboard,peg,peg,empty,peg},
{peg,peg,peg,peg,peg},
};
//cout<<pvect[0][5];
//use the setter to set the vect data member
board a(pvect);
a.display();
if(a.control_vect()==true){
cout<<"a";
}
else{
cout<<"b";
}
//lets print out the elements of the data member vect for the object myBoard using the display() member function
// myBoard.display();
return 0;
}
我编写了上行代码并运行。然后我在 control_vect 函数中尝试更复杂的条件,该函数不给出返回值。但最不复杂的 control_funcion 返回一个布尔值并打印 vect[0][0] 元素。但不利的一面不打印 bool 或 vect[0][0]。为什么底层函数不返回 bool 值?虽然类似。另外我正在尝试循环并打印所有向量二的运行。示例输出:İf 条件提供打印 a,不要通过程序提供 print b。
bool board :: control_vect(){
long unsigned int i,j;
for (i = 0; i < vect.size(); i++){
for ( j = 0; j < vect[i].size(); j++){ //Look for legal moves on the board.If there are legal moves program will continue.
if(vect[i][j]==1 && vect[i][j+1]==1 && vect[i][j+2]==2){
controller=true;
break;
}
else if(vect[i][j]==2 && vect[i][j+1]==1 && vect[i][j+2]==1){
controller=true;
break;
}
else if(vect[i][j] == 1 && vect[i+1][j] == 1 && vect[i+2][j] == 2){
controller=true;
break;
}
else if (vect[i][j] == 2 && vect[i+1][j] == 1 && vect[i+2][j] == 1){
controller=true;
break;
}
}
}
return controller;
}
解决方案
我在您给定的代码片段中发现了一些错误。
错误 1
您正在使用using namespace std
,因此这将导致错误提示
error: reference to 'empty' is ambiguous
86 | {noboard,peg,peg,empty,peg}
可以在这里看到。
错误1的解决方案
您可以通过替换来解决此问题{noboard,peg,peg,empty,peg}
:
{noboard,peg,peg,type::empty,peg} //note the type:: i have added
现在您的程序可以正常工作,如下所示。
错误 2
您给出的第二个代码片段没有controller
在内部定义变量bool board :: control_vect()
,它也没有cout<<vect[0][0];
错误 2 的解决方案control_vect()
您可以通过在's 的正文
中添加以下语句来解决这些问题:
bool controller = false;
cout << vect[0][0];
所以正确的修改代码如下所示:
bool board :: control_vect(){
long unsigned int i,j;
bool controller = false; //note I HAVE ADDED THIS
cout << vect[0][0]; //note I HAVE ADDED THIS
for (i = 0; i < vect.size(); i++){
for ( j = 0; j < vect[i].size(); j++){ //Look for legal moves on the board.If there are legal moves program will continue.
if(vect[i][j]==1 && vect[i][j+1]==1 && vect[i][j+2]==2){
controller=true;
break;
}
else if(vect[i][j]==2 && vect[i][j+1]==1 && vect[i][j+2]==1){
controller=true;
break;
}
else if(vect[i][j] == 1 && vect[i+1][j] == 1 && vect[i+2][j] == 2){
controller=true;
break;
}
else if (vect[i][j] == 2 && vect[i+1][j] == 1 && vect[i+2][j] == 1){
controller=true;
break;
}
}
}
return controller;
}
错误 3
您试图访问vect[i][j+2]
,vect[i+2][j]
但您忘记从循环变量值中减去 2,从而导致分段错误,如此处所示。
for (i = 0; i < vect.size(); i++){//you forgot to subtract 2
for ( j = 0; j < vect[i].size(); j++){//you forgot to subtract 2
错误 3 的解决方案您可以通过从循环变量中减去 2 来解决此问题,i
如下j
所示:
for (i = 0; i < vect.size() -2; i++){ //note i have subtracted 2
for ( j = 0; j < vect[i].size()-2; j++){//note i have subtracted 2
所以最终正确修改的代码如下所示:
bool board :: control_vect(){
long unsigned int i,j;
bool controller = false;
cout << vect[0][0];
for (i = 0; i < vect.size() -2; i++){// note I HAVE SUBTRACTED 2
for ( j = 0; j < vect[i].size()-2; j++){ //note I HAVE SUBTRACTED 2 //Look for legal moves on the board.If there are legal moves program will continue.
if(vect[i][j]==1 && vect[i][j+1]==1 && vect[i][j+2]==2){
controller=true;
break;
}
else if(vect[i][j]==2 && vect[i][j+1]==1 && vect[i][j+2]==1){
controller=true;
break;
}
else if(vect[i][j] == 1 && vect[i+1][j] == 1 && vect[i+2][j] == 2){
controller=true;
break;
}
else if (vect[i][j] == 2 && vect[i+1][j] == 1 && vect[i+2][j] == 1){
controller=true;
break;
}
}
}
return controller;
}
control_vect()
现在您的作品和印刷品的第二个代码片段bool
以及vect[0][0]
您想要的并且可以在这里看到。完整的工作程序也可以在这里看到。
推荐阅读
- java - Kotlin:如果前面没有数字,如何不重复数学运算?
- r - 包 Rmpi 已安装,但 R 返回“正在加载所需的包:Rmpi”
- python - 将带有 docker 的烧瓶应用程序部署到 heroku 时出现错误 R10
- java - 什么是最新的、兼容的 Jars 集,用于在 Eclipse 中使用 Cucumber
- c - 通过将消息传递给“配置”来实现 mac 自动化
- katex - 如何使用 Katex 围绕分数(“bbox”)制作背景框?
- node.js - 读取csv时如何计算行/循环
- r - Tidyr:删除字符串直到某个字符
- angular - 错误:未找到 [object Object] 的组件工厂。你把它添加到@NgModule.entryComponents 了吗?角 7
- python - 如何根据列条件将选定列从数据框中复制到另一个