0

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
Alex D Rex
  • 89
  • 1
  • 9
  • 2
    If the compiler says it hasn't been declared, claiming "I declared it here" in a comment about an (unseen) header doesn't help. You *think* you have declared it, the compiler tells you you haven't. Show us the header. Better even, trim down both header and source file until *all* they do is reproducing the error (i.e., a [mcve]), and post *that*. (Unless you have found the problem in the trimming-down process, which is likely.) – DevSolar Nov 28 '17 at 08:14
  • 1
    quoting François Andrieux - Consider providing a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Your example requires specific files and we can't guess what they contain. If we could try your code ourselves, it would make it much easier to identify the problem. Using significant names can also help understand and reason about your code. – JTejedor Nov 28 '17 at 08:15
  • 1
    Try: after #include write class Game; – Tazo leladze Nov 28 '17 at 08:19
  • sir that doesnt help – Alex D Rex Nov 28 '17 at 08:27
  • as you can see i have provided that sir, – Alex D Rex Nov 28 '17 at 08:48
  • No you haven't. Please provide the contents of `game.h`, or at least the point where you *think* you have provided a declaration of `Game`. (@Taz742: What good would that do? If his `` does not provide a declaration of `Game`, addung a forward declaration wouldn't really help either.) – DevSolar Nov 28 '17 at 08:52
  • sir i have edit the post and provided the game.h file – Alex D Rex Nov 28 '17 at 08:57
  • Possible duplicate of [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – king_nak Nov 28 '17 at 09:14
  • thank you sir, for your help, nw i pointed it out – Alex D Rex Nov 28 '17 at 09:40

1 Answers1

4

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.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436