Qt:"Expected type-specifier"出现在一台设备上,而不是另一台具有相同代码的设备上

Qt: "Expected type-specifier" appearing on one device and not another with same code

本文关键字:一台 代码 type-specifier Expected Qt      更新时间:2023-10-16

我和一位朋友是C++和Qt的初学者。我们正在一起开发一个Qt项目(直升机游戏),当她向我发送她在计算机上的代码时,该代码运行没有错误,它在一行上给出了"预期的类型说明符"错误,该行将指针设置为等于我们的"游戏"构造函数的新实例。代码有问题还是与我的计算机有关?

这就是我们的错误出现在主函数中的地方:

#include <QApplication>
#include <QGraphicsItem>
#include <QGraphicsPixmapItem>
#include <QGraphicsView>
#include <QGraphicsScene>
//including for splash screen
#include <QSplashScreen>
#include <QTimer>
//our header files
#include "player.h"
#include "game.h"
game * heli_game;
int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    QSplashScreen * splash = new QSplashScreen;
    splash->setPixmap(QPixmap(":/images/splash.png"));
    splash->show();
    game * heli_game;
    heli_game = new game::game();   //ERROR HERE: "expected type-specifier"

    QTimer::singleShot(2500, splash, SLOT(close()));
    return a.exec();
}

这是我们的游戏头文件,其中定义了构造函数:

#ifndef GAME
#define GAME
#include <QGraphicsView>
#include <QWidget>
#include <QGraphicsScene>
#include "player.h"
#include "score.h"
//#include "game_over.h"
class game: public QGraphicsView{
    Q_OBJECT
public:
    //constructors
    game(QWidget* parent=NULL);
    //public attributes
    QGraphicsScene * scene;
    player * copter;
    score * Score;
//  game_over * game_over;
};
#endif // GAME
new game::game(); 

^ 仅当game类位于名为 game 的命名空间中时,这才有意义。 由于不是,编译器无法知道您指的是哪种类型。

您可能打算这样做:

new game();

我还应该指出,heli_game在您的主文件中声明了两次。

heli_game = new game::game();

不正确,应该是

heli_game = new game();

只。您的代码不应使用任何编译器进行编译。

顺便说一下,你有一个"heli_game *game"全局变量和另一个在main()中。