错误:无法将'std :: string(*)[3]'转换为'std :: string **作为回报

时间:2017-08-11 03:16:32

标签: c++ arrays string

努力让我的代码编译,我遇到了一个奇怪的错误,我之前没见过。我正试图让Board::getAr()返回ar,但我收到了这个错误。 120行。

  1 #include <iostream>
  2 #include "Board.hpp"
  3 #include <string>
  4 #include <cstring>
  5
  6 Board::Board()
  7 {
  8         for(int j = 0;j < 1;j++)
  9         {
 10                 for(int i = 0;i < 4;i++)
 11                 {
 12                         ar[i][j] = i;
 13                 }
 14         }
 15         for(int j = 0;j < 4;j++)
 16         {
 17                 for(int i = 0;i < 1;i++)
 18                 {
 19                         ar[i][j] = j;
 20                 }
 21         }
 22         for(int j = 1;j < 4;j++)
 23         {
 24                 for(int i = 1;i < 4;i++)
 25                 {
 26                         ar[i][j] = ".";
 27                 }
 28         }
 29 }
 30 bool Board::makeMove()
 31 {
 32         if(ar[x][y] == ".")
 33         {
 34                 return true;
 35         }
 36         else
 37         {
 38                 std::cout << "That square is already taken." << std::endl;
 39                 return false;
 40         }
 41 }
 42 boardState Board::gameState()
 43 {
 44         bool temp = gameCheck();
 45         if(temp == true)
 46         {
 47                 if(player == 'x')
 48                 {
 49                         return X_WON;
 50                 }
 51                 else if(player == 'o')
 52                 {
 53                         return O_WON;
 54                 }
 55         }
 56         else
 57         {
 58                 return UNFINISHED;
 59         }
 60 }
 61 bool Board::gameCheck()
 62 {
 63         bool row = true;
 64         bool col = true;
 64         bool col = true;
 65         bool diag = true;
 66         bool altDiag = true;
 67
 68         for(int j = 1;j < 4;j++)
 69         {
 70                 for(int i = 1;i < 4;i++)
 71                 {
 72                         const char* temp1 = ar[i][j].c_str();
 73                         const char* temp2 = ar[j][i].c_str();
 74                         const char* temp3 = ar[i][i].c_str();
 75                         const char* temp4 = ar[i][4-1].c_str();
 76                         if(*temp1 != player)
 77                         {
 78                                 row = false;
 79                         }
 80                         if(*temp2 != player)
 81                         {
 82                                 col = false;
 83                         }
 84                         if(*temp3 != player)
 85                         {
 86                                 diag = false;
 87                         }
 88                         if(*temp4 != player)
 89                         {
 90                                 altDiag = false;
 91                         }
 92                 }
 93         }
 94         if(row == true || col == true || diag == true || altDiag == true)
 95         {
 96                 return true;
 97         }
 98         else
 99         {
100                 return false;
101         }
102 }
103 void Board::print()
104 {
105         for(int j = 0;j < 4;j++)
106         {
107                 for(int i = 0;i < 4;i++)
108                 {
109                         std::cout << ar[i][j] << " ";
110                 }
111                 std::cout << "\n";
112         }
113 }
114 char Board::getPlayer()
115 {
116         return player;
117 }
118 std::string** Board::getAr()
119 {
120         return ar;
121 }
122 void Board::setX(int a)
123 {
124         x = a;
125 }
126 void Board::setY(int b)
127 {
128         y = b;
129 }

有什么想法吗?在修复此问题之前,我无法启动逻辑故障排除。这是我的hpp文件:

  1 #ifndef BOARD_HPP
  2 #define BOARD_HPP
  3
  4 #include <string>
  5
  6 enum boardState {X_WON, O_WON, DRAW, UNFINISHED};
  7
  8 class Board
  9 {
 10 public:
 11         int x, y;
 12
 13         Board();
 14         bool makeMove();
 15         boardState gameState();
 16         void print();
 17         char getPlayer();
 18         std::string** getAr();
 19         void setX(int);
 20         void setY(int);
 21         bool gameCheck();
 22 private:
 23         std::string ar[3][3];
 24         char player;
 25 };
 26 #endif

我很丢失,任何其他类似的线程都没有帮我解决问题。

0 个答案:

没有答案