简体   繁体   中英

getting an declaration error in header file

i am developing a breakout game using C++ in qt creator. i am getting an error saying "Game has not been declared". i have declared it using the game.h the error is in the header file. i cannot figure out where the problem is. please, any help would be highly appriciated.

#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

and this is the fuction of the CPP file

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

}

sir this is my game.h file

#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

You have a cyclic dependency. Ball.h includes game.h, and game.h includes Ball.h. This is an impossible situation for the compiler to resolve, as neither one can be included before the other.

It appears that game.h does not need to #include "Ball.h" . Instead, use a forward declaration:

class Ball;

That should be enough to compile game.h.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM