在头文件中获取声明错误

时间:2017-11-28 08:10:15

标签: c++ qt

我正在使用qt creator中的C ++开发一款突破游戏。我收到一条错误,说“游戏尚未宣布”。我已经使用game.h声明了它。错误在头文件中。我无法弄清楚问题出在哪里。拜托,任何帮助都会受到高度关注。

#ifndef BALL_H
#define BALL_H
#include<QCloseEvent>
#include <QGraphicsRectItem>
#include "game.h"   //i have declared it here.




class Ball: public QObject, public QGraphicsRectItem{
Q_OBJECT
public:
// constructors
Ball(QGraphicsItem* parent=NULL);
QTimer *runTimer;


// public methods
double getCenterX();


public slots:
// public slots
void move();
void start_timer();
void stop_timer();
void call_game_fuction(Game *gm);  //here i am getting the error(Game)


private:
// private attributes
double xVelocity;
double yVelocity;
int counter = 0;

// private methods

void resetState();
bool reverseVelocityIfOutOfBounds();
void handlePaddleCollision();
void handleBlockCollision();
};

#endif // BALL_H

这是CPP文件的功能

Game *obj1 =new Game();
           game_function *obj2 = new game_function();
void Ball::call_game_fuction(Game *gm)
{
gm->set_background();

}
先生,这是我的game.h文件

#ifndef GAME_H
#define GAME_H

#include <QGraphicsView>
#include <QGraphicsScene>
#include "Ball.h"
#include "Paddle.h"
#include "Block.h"
#include<QPushButton>

class Game:public QGraphicsView{

Q_OBJECT

public:
// constructors
Game(QWidget* parent=0);
QPushButton *button;
QPushButton *button1;




// public methods




void start();
void createBlockCol(double x);
void creatBlockGrid();
void set_background();
void background_Gamewon();
void set_buttons();

QGraphicsScene* scene2;




// public attributes
QGraphicsScene* scene;
QGraphicsView* view;


private slots:
void startgame();
void stopgame();





private:
bool gameOver;
Ball *ball;
Paddle *pad;
Block *bl;


};

#endif // GAME_H

1 个答案:

答案 0 :(得分:4)

你有一个循环依赖。 Ball.h包括game.h,game.h包括Ball.h.这是编译器无法解决的情况,因为任何一个都不能包含在另一个之前。

看来game.h不需要#include "Ball.h"。相反,使用前向声明:

class Ball;

这应该足以编译game.h。

相关问题