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.
Related
Im new with Qt and im making a game in it. I want to display icon but after putting there some code i cant find it anywhere. Im confused because i dont get any errors, so im pretty sure that i just didnt add something. Help.
Combat.h file
#ifndef COMBAT
#define COMBAT
#include <QImage>
#include <QGraphicsPixmapItem>
class Combat: public QGraphicsPixmapItem{
public:
// constructors
//Combat(QPixmap *parent=NULL);
Combat(const QString x);
// setters/getters
void getOwner(QString x);
private:
QString owner;
};
Combat.cpp
#include "Combat.h"
#include <QGraphicsScene>
Combat::Combat(const QString x){
// draw graphics
setPixmap(QPixmap(x));
}
void Combat::getOwner(QString x){
owner = x;
}
#endif // COMBAT
Interface.h
#ifndef INTERFACE
#define INTERFACE
#include <QList>
#include "Hex.h"
#include "Combat.h"
class Interface{
public:
// constructors
Interface();
// getters/setters
QList<Hex*> getHexes();
void getOwner(int x);
// public methods
void placeHexes();
void placeCombat();
private:
void createHexColumn(int x, int y, int numOfRows);
QList<Hex*> hexes;
void createCombatIcon(int x, int y, QString z);
int owner;
};
#endif // INTERFACE
Interface.cpp
#include "Interface.h"
#include "Game.h"
extern Game* game;
Interface::Interface(){
}
QList<Hex *> Interface::getHexes(){
return hexes;
}
void Interface::placeHexes(){
createHexColumn(100,100,6);
}
void Interface::placeCombat()
{
createCombatIcon(100,100,":/grafika/atak_magiczny.png");
}
void Interface::createCombatIcon(int x, int y, QString z)
{
Combat* icon = new Combat(z);
icon->getOwner("player1");
icon->setPos(x,y);
game->scene->addItem(icon);
}
Im not sure where something is missing
I solved the problem by adding my graphics to resource file in Qt Creator...
I've encountered an error which I cannot seem to find a solution to anywhere else.
The error occurs when I'm trying to declare an instance of the "EncodeWindow" class.The compiler is giving errors C2143,C4430 and C2238. I am simply trying to give the "MainWindow" class an instance of "EncodeWindow".
File mainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
#include <QLabel>
#include "Image.h"
#include "encodewindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
/*Check whether a given file path is valid*/
bool CheckFilePath(QString);
/*Sets UI widgets*/
void setOriginalImage_Label(const char*);
void setEncodedImage_Label(const char*);
void setDebug_TextBox(QString);
/*Saves all information about current encoding/decoding*/
void saveFile();
private slots:
void on_Encode_Button_clicked();
void on_Reset_Button_clicked();
void on_Save_Button_clicked();
void on_AddEncodeImage_Debug_Button_clicked();
void on_AddImage_Button_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
EncodeWindow *CurrentEncodeWindow = new EncodeWindow; //ERROR HERE!! C2143
int fileNumber = 0;
};
#endif // MAINWINDOW_H
File encodeWindow.h:
#ifndef ENCODEWINDOW_H
#define ENCODEWINDOW_H
#include <QMainWindow>
#include "mainwindow.h"
namespace Ui {
class EncodeWindow;
}
class EncodeWindow : public QMainWindow
{
Q_OBJECT
public:
explicit EncodeWindow(QWidget *parent = 0);
~EncodeWindow();
/*Get filepaths from 'result' object (For access to filepaths without 'encode' Window)*/
const char* getOriginalFilePath();
const char* getEncodeFilePath();
const char* getSaveFilePath();
bool checkUI(const char*,const char*,const char*,bool*);
bool CheckFilePath(const char*);
std::string readInText(const char*);
private slots:
void on_RedChannel_CheckBox_clicked(bool);
void on_GreenChannel_CheckBox_clicked(bool);
void on_BlueChannel_CheckBox_clicked(bool);
void on_AlphaChannel_CheckBox_clicked(bool);
void on_BitDepth_Slider_sliderMoved(int);
void on_BitDepth_SpinBox_valueChanged(int);
void on_Encode_Button_clicked();
private:
Ui::EncodeWindow *ui;
Encoded result; //Enocoded object (child of Image class)
};
#endif // ENCODEWINDOW_H
Any help would be great. The code was done in Qt for an image steganography project.
Thanks.
You have circular includes. "mainwindow.h" includes "encodewindow.h" and "encodewindow.h" includes "mainwindow.h". The include guards stop the cycle, but the class definitions for whichever header is included first won't be complete in the second include file, which will result in your errors.
In the snippet above, there's nothing that I see in encodewindow.h that uses anything in mainwindow.h, so just remove the include of mainwindow.h from encodewindow.h.
I am trying to create QQuickWidget based application.
What i am trying to do:
Class A(game.h) and Class B(gamestate.h) are forward declared. Class A is main QQuickWidget class with methods. Class B QObject derived class contains signals, slots, variables and methods.
Class B Variable values can set from class A -- Working
When variable value changes signal should be emitted -- working
when signal was emitted slot method should be called in class B -- working
Class B should invoke a method in class A -- working
Class A should create another qquickwidget -- NOT WORKING
(No compiling error. Application crashes on load)
I tried to call from class A and showIntro() function working fine. But when tried to call from class B its not working.
Game.h
#ifndef GAME_H
#define GAME_H
#include <QQuickWidget>
class GameState;
class Game: public QQuickWidget
{
Q_OBJECT
public:
Game();
GameState *gameState;
void showIntro();
public slots:
void onStatusChanged(QQuickWidget::Status);
};
#endif // GAME_H
Game.cpp
#include "game.h"
#include <QQuickWidget>
#include <QDebug>
#include "gamestate.h"
Game::Game(): QQuickWidget()
{
gameState = new GameState(this);
mainScreen = new QQuickWidget();
connect(this, SIGNAL(statusChanged(QQuickWidget::Status)), this, SLOT(onStatusChanged(QQuickWidget::Status)));
setFixedSize(450, 710);
setSource(QUrl("qrc:/EmptyScreen.qml"));
}
void Game::onStatusChanged(QQuickWidget::Status status)
{
switch(status)
{
case QQuickWidget::Ready:
qDebug() << "hi";
gameState->setValue(1);
//showIntro();
break;
case QQuickWidget::Error:
qDebug() << "Error";
break;
}
}
void Game::showIntro()
{
mainScreen->setSource(QUrl("qrc:/MainScreen.qml"));
mainScreen->setAttribute(Qt::WA_TranslucentBackground);
mainScreen->setParent(this);
}
Here is my Gamestate.h
#ifndef GAMESTATE_H
#define GAMESTATE_H
#include <QObject>
class Game;
class GameState : public QObject
{
Q_OBJECT
public:
explicit GameState(QObject *parent = 0);
int value() const {return m_value; }
Game *game;
signals:
void valueChanged(int newValue);
public slots:
void setValue(int value);
void stateChanged(int value);
private:
int m_value;
};
#endif // GAMESTATE_H
GameState.cpp
#include "gamestate.h"
#include "game.h"
GameState::GameState(QObject *parent) : QObject(parent)
{
m_value = 0;
connect(this,SIGNAL(valueChanged(int)), this, SLOT(stateChanged(int)));
}
void GameState::setValue(int value)
{
if(value != m_value)
{
m_value = value;
emit valueChanged(value);
}
}
void GameState::stateChanged(int value)
{
if(value == 1)
{
game->showIntro();
}
}
and my final main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include "game.h"
Game *game;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
game = new Game();
game->show();
return app.exec();
}
Pls suggest me what could be the issue.
Member variable Game* game of class GameState is not initialized and therefore the program crashes when trying to dereference the pointer within GameState::stateChanged().
Change the constructor of GameState to the following:
// in gamestate.h
explicit GameState(Game *parent = 0);
// in gamestate.cpp
GameState::GameState(Game *parent) : QObject(parent), game(parent)
{
m_value = 0;
connect(this,SIGNAL(valueChanged(int)), this, SLOT(stateChanged(int)));
}
I'm getting the error "expected class-name before '{' token" in my C++ Qt project. After googling it, it seems like its a problem of circular includes. I have pawn.h that includes piece.h, which includes board.h, which completes the circle by including pawn.h. I've read that this can be fixed with forward declarations, but I've tried forward declaring a few of problem classes, and it doesn't work.
#ifndef PAWN_H
#define PAWN_H
#include "piece.h"
class Pawn : public Piece
{
Q_OBJECT
public:
explicit Pawn(QWidget *parent = 0);
};
#endif // PAWN_H
.
#ifndef PIECE_H
#define PIECE_H
#include <QWidget>
#include "board.h"
class Board;
class Piece : public QWidget
{
Q_OBJECT
public:
explicit Piece(QWidget *parent = 0);
void setPosition(int rank, int file);
QPixmap pixmap;
protected:
void paintEvent(QPaintEvent *);
private:
int rank;
int file;
int x;
int y;
};
#endif // PIECE_H
.
#ifndef BOARD_H
#define BOARD_H
#include <QWidget>
#include <QVector>
#include <QGridLayout>
#include "square.h"
#include "pawn.h"
#include "knight.h"
#include "bishop.h"
#include "queen.h"
#include "king.h"
class Board : public QWidget
{
public:
explicit Board(QWidget *parent = 0);
QVector < QVector<Square *> > sqrVector;
Pawn *pawn[8];
Knight *knight[2];
Bishop *bishop[2];
Queen *queen;
King *king;
private:
QGridLayout *layout;
};
#endif // BOARD_H
Instead of randomly trying things, try changing board.h to include forward declarations for all the pieces:
board.h
class Pawn;
class Knight;
class Bishop;
class Queen;
class King;
And remove the corresponding #include statements. (You'll probably need to put those #include statements in board.cpp, when you decide you need to see inside the various piece classes.)
Your main problem lays in the file: piece.h. Since board is not referenced explicitly in the file whatsoever, the include for it and the forward declaration should be removed. That will break the circle. Additionally, as Greg pointed out, only forward declarations are needed in board.h.
Im having a problem of my Rectangle class not being seen as a type. I've included the proper header, and so I am confused.
shapes.h
#ifndef SHAPES_H
#define SHAPES_H
#include "Colors.h"
#include <QPoint>
#include "glwidget.h"
//class GLWidget;
class Shape
{
public:
virtual void draw();
};
class Rectangle : Shape
{
public:
Rectangle(GLWidget *w, QPoint tl, QPoint br){
glWidget = w;
topLeft = tl;
btmRight = br;
}
virtual void draw(){
// top horizontal
for(int i = topLeft.x(); i < btmRight.x(); i++){
glWidget->setPixel(i,topLeft.y(), color);
}
}
private:
QPoint topLeft,btmRight;
GLWidget *glWidget;
RGBColor color;
};
#endif // SHAPES_H
glwidget.cpp
#include <QtGui>
#include <QtOpenGL>
#include <math.h>
#include <stdio.h>
#include "glwidget.h"
#include "Shapes.h"
#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE 0x809D
#endif
// ... a bunch of code that doesn't need to be included
void GLWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
// do some drawing stuff
QPoint mPos = event->pos();
switch(drawmode)
{
case 1:
currentShape = new Rectangle(this,mPos, mPos); /*** This is the error ***/
}
}
}
glwidget.h
#ifndef AGLWIDGET_H
#define AGLWIDGET_H
#include <QGLWidget>
#include "Colors.h"
class Shape;
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
GLWidget(QWidget *parent = 0);
~GLWidget();
QSize minimumSizeHint() const;
QSize sizeHint() const;
void setPixel(int x, int y, RGBColor c);
public slots:
void setColor(RGBColor c);
void setDrawRectangle();
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private:
QPoint lastPos;
QVector<QPoint> drawPoints;
RGBColor paintColor;
int drawmode;
Shape *currentShape;
};
Sorry for the load of code... the exact error is
'Rectangle' is not a type glwidget.cpp line 85
Anybody have an idea why it wouldn't be seeing Rectangle as a type in glwidget.cpp despite my including "Shapes.h"?
Thanks in advance!
This is a bit of a longshot, but are you sure you're using moc correctly in regards to the GLWidget code? IE, have you added #include "glwidget.moc to the .cpp file or included it in your build system (qmake knows to do this for you), as well as running moc first. I only mention this because forgetting to do this many moons ago caused me to see a pile of inscrutable type-related warnings and errors.
Perhaps somewhere in the ancestry of GLWidget there is a method or member called Rectangle and there is a confusion. See the documentation for GLWidget and its ancestors
Looks like the compiler believes Rectangle is a template
Well I'm gonna go with it had something to do with the virtual function within Shape not being defined as in g++ undefined reference to typeinfo. The machine I had the strange error on is using an older version of Qt than I have on my personal machine, and my personal is having no issues with this code.
Thanks for the suggestions everyone, but I'm just gonna put this one to rest.