[英]error: expected expression before { in c++
I am trying to initialize a one dimensional array as a member variable inside a class but I'm getting an expected expression error 我正在尝试将一维数组初始化为类中的成员变量,但出现了预期的表达式错误
here is the declaration inside the tetris.h file: 这是tetris.h文件中的声明:
private:
int* rotate(char piece,int rot);
int width;
int* heights;
char* data[0];
const string blockkeys;
int *blocks;
};
and here is the constructor: 这是构造函数:
Tetris::Tetris(int w):blockkeys("IOJLZST"){
blocks = new int[7*4*2]={
0,0,0,1,0,2,0,3,
0,0,0,1,1,1,1,0,
0,0,1,0,1,1,1,2,
0,0,1,0,0,1,0,2,
0,0,1,0,0,1,-1,1,
0,0,1,0,1,1,2,1,
0,0,-1,1,1,1,2,1};
width=w;
heights=new int[width];
char *data[width];
for (unsigned int i=0; i<width; ++i){
data[i]=new char[0];
heights[i]=0;
}
}
the error is happening immediately after "new int[7*4*2]=" and before "{" 该错误在“ new int [7 * 4 * 2] =“之后和“ {”之前立即发生
Did you check the syntax? 您检查语法了吗? I'm afraid "new int[7*4*2]=anything" is not a valid initializing expression in C++. 恐怕“ new int [7 * 4 * 2] = anything”在C ++中不是有效的初始化表达式。
Also a zero-length array declaration is not valid. 零长度数组声明也是无效的。
EDIT After OP explanation: 编辑 OP解释后:
Try declare the array static: 尝试声明数组为静态:
class Tetris {
....
static int blocks[7*4*2];
};
int Tetris::blocks[7*4*2]={
0,0,0,1,0,2,0,3,
0,0,0,1,1,1,1,0,
0,0,1,0,1,1,1,2,
0,0,1,0,0,1,0,2,
0,0,1,0,0,1,-1,1,
0,0,1,0,1,1,2,1,
0,0,-1,1,1,1,2,1};
then use blocks
in Tetris
member functions. 然后在Tetris
成员函数中使用blocks
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.