c - “错误:预期的';',','或')'在数字常量之前”出现在我的代码中
问题描述
当我尝试运行此 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
解决方案
#define COLSIZE 4
#define ROWSIZE 5
void creatGameMatrix(int* mat, int ROWSIZE , int COLSIZE);
这是没有意义的。它扩展到
void creatGameMatrix(int* mat, int 5, int 4);
这显然是不对的(特别是函数参数名称必须是标识符,4
并且5
不是标识符)。选项是:
- 不要传递参数并在例程中使用#defines
- 将参数命名为
rowsize
andcolsize
并在例程中使用这些参数。ROWSIZE
使用和COLSIZE
定义传递值。
推荐阅读
- database - 非关键属性的唯一性是否违反 3NF?
- angular - 如何在Angular 7中将数据从孩子传递给父母
- spring - Spring MVC 项目中的空指针异常
- javascript - 如何通过我已经制作的网站向我的安卓应用发送通知,这不是谷歌的网络应用
- android - Kotlin 崩溃应用程序问题 SetOnCreate 方法
- assembly - MIPS 中的符号地址与标签
- python - 根据 rssi 值通过蓝牙发送文件
- python - 如何修复 Conda“缺少环境变量路径”?
- c++ - 早期 C 和 C++ 编译器中乘法和除法的手动位移有多相关?
- java - 由于 FXML 文件中的控制器更改导致线程异常