命令行参数错误

时间:2014-03-09 22:36:15

标签: c++ matrix const command-line-arguments

我正在尝试将命令行参数传递到我的C ++文件中,该文件确定游戏Reversi的矩阵大小。对于我的生活,我无法弄清楚为什么它不起作用。

#include <iostream>
#include <cstdlib>
using namespace std;



const char player1='X';
const char player2='O';

struct Dimension
{
    int size;
};

char PrintBoard(char board[][mat.size]);


char PrintBoard(char board[][mat.size]){
  //if(boardSize % 2 == 0 && boardSize >= 4 && boardSize <= 10){

  for(int i =  0; i < mat.size; i++){
    for(int j = 0; j < mat.size; j++){
      board[i][j]='-';
    }
  }
  for(int i = 0;i < mat.size; i++){
    for(int j = 0; j < mat.size; j++){
      cout<<board[i][j];
    }
    cout<<endl;
  }

  return board[mat.size][mat.size];
}
//else{
//cout<<"The board must be an even size between 4 and 10."<<endl;
//}



int main(int argc, char*argv[]){
  Dimension mat;
  mat.size=atoi(argv[1]);
  char board[mat.size][mat.size];
  board[mat.size][mat.size]=PrintBoard(board);
  int i=1;
  while(i<mat.size*mat.size){
    char pawn;
    char oppo;
    if(i%2 != 0){
      pawn=player1;
      oppo=player2;
    }
    else
      {
      pawn=player2;
      oppo=player1;
      }


    cout<<pawn<<"'s turn: "<<endl;
    cin>>row>>col;

    board[row-1][col-1]=pawn;

    //RIGHT
        if(board[row-1][col]==oppo && board[row-1][col+1] != '-' && board[row-1][col+1] != pawn)
    {
        int c=col;
        while(board[row-1][c]!=pawn && board[row-1][c] != '-')
        {
            board[row-1][c]=pawn;
            c++;
        }
    }

    //LEFT
        if(board[row-1][col-2]==oppo && board[row-1][col-3] != '-' && board[row-1][col-3] != pawn)
    {
        int c=col-2;
        while(board[row-1][c]!=pawn && board[row-1][c] != '-')
        {
            board[row-1][c]=pawn;
            c--;
        }
    }

    //UP
        if(board[row-2][col-1]==oppo && board[row-3][col-1] != '-' && board[row-3][col-1] != pawn)
    {
        int r=row-2;
        while(board[r][col-1]!=pawn && board[r][col-1] != '-')
        {
            board[r][col-1]=pawn;
            r--;
        }
    }

        //DOWN
        if(board[row][col-1]==oppo && board[row+1][col-1] != '-' && board[row+1][col-1] != pawn)
    {
        int r=row;
        while(board[r][col-1]!=pawn && board[r][col-1] != '-')
        {
            board[r][col-1]=pawn;
            r++;
        }
    }

        //DOWN-RIGHT
        if(board[row][col]==oppo && board[row+1][col+1] != '-' && board[row+1][col+1] != pawn)
        {
            int r=row;
            int c=col;
            while(board[r][c] != pawn && board[r][c] != '-' && r-1 != -1 && c-1 != -1 && r != mat.size+1 and c != mat.size+1)
            {
                board[r][c]=pawn;
                r++;
                c++;
            }
        }

        //DOWN-LEFT
        if(board[row][col-2]==oppo && board[row+1][col-3] != '-' && board[row+1][col-3] != pawn)
        {
                int r=row;
                int c=col-2;
                while(board[r][c] != pawn && board[r][c] !='-' && r-1 != -1 && c-1 != -1 && r != mat.size+1 and c != mat.size+1)
                {
                    board[r][c]=pawn;
                    r++;
                    c--;
                }
        }

        //UP-RIGHT
        if(board[row-2][col]==oppo && board[row-3][col+1] != '-' && board[row-3][col+1] != pawn)
        {
            int r=row-2;
            int c=col;
            while(board[r][c] != pawn && board[r][c] !='-'&& r-1 != -1 && c-1 != -1 && r != mat.size+1 and c != mat.size+1)
            {
                board[r][c]=pawn;
                r--;
                c++;
            }
        }

        //UP-LEFT
        if(board[row-2][col-2]==oppo && board[row-3][col-3] != '-' && board[row-3][col-3] != pawn)
        {
            int r=row-2;
            int c=col-2;
            while(board[r][c] != pawn && board[r][c] !='-' && r-1 != -1 && c-1 != -1 && r != mat.size+1 and c != mat.size+1)
            {
                board[r][c]=pawn;
                r--;
                c--;
            }
        }




    //PRINTTTT
    for(int h = 0;h < mat.size; h++){
      for(int j = 0; j < mat.size; j++){
    cout<<board[h][j];
      }
      cout<<endl;

    }

    i++;
}  
    int o_num=0;
    int x_num=0;
    for(int r=0; r<mat.size; r++)
    {
        for(int c=0; c<mat.size; c++)
        {
            if(board[r][c]=='O')
                o_num++;
            else if(board[r][c]=='X')
                x_num++;
            else
                ;
        }
    }

    cout<<"Number of O's: "<<o_num<<endl;
    cout<<"Number of X's: "<<x_num<<endl;

    if(o_num>x_num)
        cout<<"O Wins!"<<endl;
    else if(o_num<x_num)
        cout<<"X Wins!"<<endl;
    else
        cout<<"Tie!"<<endl;


  return 0;

}

如果我将mat.size更改为const,就像ROWS一样,它可以正常运行,但只有当我尝试将其作为命令行参数时,它才会抱怨。有人可以帮助找到问题吗?我几乎在我的智慧结束。非常感谢你的进步!

编辑: 这是编译器错误。抱歉,之前应该添加此内容。

board.cpp:14:30: error: ‘mat’ was not declared in this scope
 char PrintBoard(char board[][mat.size]);
                              ^
board.cpp:17:30: error: ‘mat’ was not declared in this scope
 char PrintBoard(char board[][mat.size]){
                              ^
board.cpp: In function ‘char PrintBoard(...)’:
board.cpp:20:23: error: ‘mat’ was not declared in this scope
   for(int i =  0; i < mat.size; i++){
                       ^
board.cpp:22:7: error: ‘board’ was not declared in this scope
       board[i][j]='-';
       ^
board.cpp:25:21: error: ‘mat’ was not declared in this scope
   for(int i = 0;i < mat.size; i++){
                     ^
board.cpp:27:13: error: ‘board’ was not declared in this scope
       cout<<board[i][j];
             ^
board.cpp:32:10: error: ‘board’ was not declared in this scope
   return board[mat.size][mat.size];
          ^
board.cpp:32:16: error: ‘mat’ was not declared in this scope
   return board[mat.size][mat.size];
                ^
board.cpp: In function ‘int main(int, char**)’:
board.cpp:61:10: error: ‘row’ was not declared in this scope
     cin>>row>>col;
          ^
board.cpp:61:15: error: ‘col’ was not declared in this scope
     cin>>row>>col;
               ^
board.cpp: In function ‘char PrintBoard(...)’:
board.cpp:33:1: warning: control reaches end of non-void function [-Wreturn-type           ]
 }
 ^

1 个答案:

答案 0 :(得分:2)

数组需要具有常量大小,在编译时确定。要使其工作,您需要使用new的动态内存分配。您可以使用char board[mat.size][mat.size]代替char* board=new char[mat.size*mat.size]

然而,这将为您提供一维数组。要将二维坐标获得到一维坐标,这是公式: int idx=(mat.size*y)+x; 因此,您需要使用board[x][y]

,而不是board[mat.size*y+x]