简体   繁体   English

命令行参数错误

[英]Command Line Argument Error

I'm trying to pass a command line argument into my C++ file that determines the size of a matrix for the game Reversi. 我正在尝试将命令行参数传递到我的C ++文件中,该文件确定游戏Reversi的矩阵大小。 For the life of me I can't figure out why it's not working. 对于我的生活,我无法弄清楚为什么它不起作用。

#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;

}

If I change mat.size to a const like ROWS it works perfectly, but only when I try to make it a command line argument it complains. 如果我将mat.size更改为类似ROWSconst它可以很好地工作,但只有当我尝试使它成为命令行参数时它才会抱怨。 Can somebody help find the problem? 有人可以帮助找到问题吗? I'm almost at my wits end. 我几乎在我的智慧结束。 Thank you so much in advanced! 先谢谢你!

EDIT: This is the compiler error. 编辑:这是编译器错误。 Sorry should have added this before. 抱歉,之前应该添加此内容。

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           ]
 }
 ^

Arrays need to have constant sizes, determined at compile-time. 数组需要具有常量大小,在编译时确定。 To make it work, you will need to use dynamic memory allocation with new . 要使其工作,您需要使用new动态内存分配。 Instead of char board[mat.size][mat.size] , you can use char* board=new char[mat.size*mat.size] . 而不是char board[mat.size][mat.size] ,你可以使用char* board=new char[mat.size*mat.size]

However, this will give you a one-dimensional array. 但是,这将为您提供一维数组。 To get two-dimensional coordinates to a one-dimensional coordinate, this is the formula: int idx=(mat.size*y)+x; 为了获得二维坐标到一维坐标,这是公式: int idx=(mat.size*y)+x; Therefore, instead of board[x][y] , you will need to use board[mat.size*y+x] . 因此,您需要使用board[mat.size*y+x]而不是board[x][y] board[mat.size*y+x]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM