error: expected expression before { in c++

error: expected expression before { in c++

我正在尝试将一维数组初始化为 class 中的成员变量,但出现预期的表达式错误

这里是 tetris.h 文件中的声明:

private:
    int* rotate(char piece,int rot);

    int width;
    int* heights;
    char* data[0];
    const string blockkeys;
    int *blocks;
};

这是构造函数:

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

在 "new int[7*4*2]=" 之后和“{”之前立即发生错误

你检查过语法了吗?恐怕 "new int[7*4*2]=anything" 在 C++ 中不是有效的初始化表达式。

零长度数组声明也是无效的。

编辑 OP解释后:

尝试将数组声明为静态:

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

然后在Tetris成员函数中使用blocks