【问题标题】:Bool function as class member conditions don't return a valueBool 函数作为类成员条件不返回值
【发布时间】:2021-11-07 12:13:07
【问题描述】:
#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]。为什么 underside 函数不返回 bool 值?虽然类似。另外我正在尝试循环并打印所有向量二的运行。示例输出:İf 条件提供打印一个,不要通过程序提供 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;
        }

【问题讨论】:

  • 请格式化您的代码,创建一个minimal reproducible example,清楚地显示预期输出和实际输出。您是否启用了编译器警告?您是否尝试使用调试器单步执行代码?底部函数不包含controller 定义,所以它根本不应该编译。
  • 您能写出上述代码的预期输出吗?它与您现在得到的实际输出有何不同?另外,请澄清您在上述代码中遇到的问题。

标签: c++ oop


【解决方案1】:

我在您给定的代码 sn-p 中发现了一些错误。

错误 1

您使用的是using namespace std,所以这将导致错误

error: reference to 'empty' is ambiguous
   86 |     {noboard,peg,peg,empty,peg}

可以看到here

错误1的解决方法 您可以通过将{noboard,peg,peg,empty,peg} 替换为:

{noboard,peg,peg,type::empty,peg} //note the type:: i have added

现在你的程序可以工作了,可以看到here

错误 2

您给出的第二个代码 sn-p 没有在 bool board :: control_vect() 内定义 controller 变量,它也没有 cout&lt;&lt;vect[0][0];

错误2的解决方法 您可以通过在control_vect() 的正文中添加以下语句来解决这些问题:

 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,导致 分段错误,如 here 所示。

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 的解决方案您可以通过从循环变量 ij 中减去 2 来解决此问题,如下所示:

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() 的第二个代码 sn-p 可以根据需要工作并打印 boolvect[0][0],并且可以看到 here。完整的工作程序也可以看到here

【讨论】:

    猜你喜欢
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    相关资源
    最近更新 更多