简体   繁体   English

错误:在C ++中{前的预期表达式

[英]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.

相关问题 C++ 上的未知错误,错误:';' 之前的预期主表达式令牌 - Unknown error on C++, error: expected primary expression before ';' token 如何在C ++中修复“错误:&#39;)&#39;标记之前的预期主表达式”? - How to fix “error: expected primary -expression before ')' token” in c++? C ++错误:&#39;。&#39;之前的预期primary-expression 代币 - C++ error: expected primary-expression before ‘.’ token c ++错误:&#39;&&#39;标记之前的预期primary-expression - c++ error: expected primary-expression before '&' token C ++-派生类-错误:“ int”之前的预期主表达式 - C++ - Deriving Classes - Error: expected primary expression before'int' 错误:C++ 中的“]”标记之前的预期主表达式 - Error: expected primary-expression before ']' token in C++ C ++-错误:“ &lt;&lt;”标记之前的预期主表达式 - C++ - error: expected primary-expression before '<<' token C ++错误:“ int”之前的预期主表达式 - C++ error: expected primary-expression before 'int' C ++错误:&#39;int&#39;之前的预期primary-expression - C++ error: expected primary-expression before ‘int’ C++ 错误:带参数的构造函数中的“*”标记之前的预期主表达式 - C++ Error: expected primary expression before '*' token in constructor with parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM