首页 > 解决方案 > “错误:预期的';',','或')'在数字常量之前”出现在我的代码中

问题描述

当我尝试运行此 c 程序时出现此错误:该程序有更多 2 个文件不会出现问题。也许问题与定义无关?我可以做一个不同的常数吗?

头文件:

#ifndef NUMBERGAME_H_
#define NUMBERGAME_H_
#define COLSIZE 4
#define ROWSIZE 5
void creatGameMatrix(int* mat,  int ROWSIZE , int COLSIZE);
void shuffleMatrix(int* mat, int ROWSIZE , int COLSIZE);
#endif /* NUMBERGAME_H_ */

文件:

#include <stdio.h>
#include "NumberGame.h"


//creat an ordered matrix N*M
void creatGameMatrix(int* mat, int ROWSIZE , int COLSIZE)
{
    int number = 1;
    for (int i = 0; i < ROWSIZE; ++i) {
        for (int j = 0; j < COLSIZE; ++j) {
            mat [i][j] = number++;
        }
    }
}

void shuffleMatrix(int* mat, int ROWSIZE , int COLSIZE)
{
    int row1, col1, row2, col2;
    do{
        row1 = 1 + rand() % (ROWSIZE) ;
        row2 = 1 + rand() % (ROWSIZE) ;
        col1 = 1 + rand() % (COLSIZE) ;
        col2 = 1 + rand() % (COLSIZE) ;
    }while (row1 == row2 || col1 == col2);

}

错误指向 ROWSIZE

标签: c

解决方案


#define COLSIZE 4
#define ROWSIZE 5
void creatGameMatrix(int* mat,  int ROWSIZE , int COLSIZE);

这是没有意义的。它扩展到

void creatGameMatrix(int* mat,  int 5, int 4);

这显然是不对的(特别是函数参数名称必须是标识符,4并且5不是标识符)。选项是:

  1. 不要传递参数并在例程中使用#defines
  2. 将参数命名为rowsizeandcolsize并在例程中使用这些参数。ROWSIZE使用和COLSIZE定义传递值。

推荐阅读