无法应对预期的标识符

时间:2013-05-25 16:41:03

标签: c++ qt

我正在制作一个俄罗斯方块。好吧,我的玻璃杯(QtGlass.h)创造了一个人物。 我想在这里使用一个参数来指定图形应该是哪个形状 取。

您能否建议我为什么参数会导致此错误:

QtGlass.h:29:23: error: expected identifier before 'L'
QtGlass.h:29:23: error: expected ',' or '...' before 'L'

我在下面的评论中显示出现此错误。 顺便说一句,如果我取消注释一个无参数变体的行, 它有效。

**Figure.h**
class Figure : public QObject {
    Q_OBJECT
...
public:
    Figure(char Shape);
    //Figure();
...
};

**Figure.cpp**
Figure::Figure(char Shape) {
//Figure::Figure() {   
    previous_shape = 1;
    colour = RED;
    ...
}

**QtGlass.h**
class QtGlass : public QFrame {
    Q_OBJECT
...
protected:
    Figure the_figure('L'); //QtGlass.h:29:23: error: expected identifier before 'L' QtGlass.h:29:23: error: expected ',' or '...' before 'L'
    //Figure the_figure;
...
};

稍后修改

当我使用它时:

class QtGlass : public QFrame {
    Q_OBJECT
    QtGlass() : the_figure('L') {}


I get this:

QtGlass.cpp:164:50: error: no matching function for call to 'Figure::Figure()'
QtGlass.cpp:164:50: note: candidates are:
Figure.h:38:5: note: Figure::Figure(char)
Figure.h:38:5: note:   candidate expects 1 argument, 0 provided
Figure.h:20:7: note: Figure::Figure(const Figure&)
Figure.h:20:7: note:   candidate expects 1 argument, 0 provided

QtGlass.cpp

QtGlass::QtGlass(QWidget *parent) : QFrame(parent) {
    key_pressed = false;
    coord_x = 5;
    coord_y = 5;
    arrow_n = 0;
    highest_line = 21;
    this->initialize_glass();
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(moveDownByTimer()));
    timer->start(1000);
}

2 个答案:

答案 0 :(得分:0)

您无法使用该语法初始化成员对象。如果您的编译器支持C ++ 11的统一初始化语法或成员变量的类内初始化,您可以这样做:

class QtGlass : public QFrame {
    Q_OBJECT
...
protected:
    Figure the_figure{'L'};
    // or
    Figure the_figure = 'L'; // works because Figure(char) is not explicit
...
};

否则你需要在QtGlass'中初始化对象。构造函数初始化列表

class QtGlass : public QFrame {
    Q_OBJECT
...
protected:
    Figure the_figure;
...
};

// in QtGlass.cpp
QtGlass::QtGlass(QWidget *parent) 
: QFrame(parent)
, the_figure('L')
{}

答案 1 :(得分:0)

您正尝试在Figure的定义中创建QtGlass的实例,但不允许这样做。您必须在the_figure的构造函数中实例化QtGlass

class QtGlass : public QFrame {
    Q_OBJECT
    QtGlass() {
        the_figure = Figure('L');
    };
...
protected:
    Figure the_figure;
...
};