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.
Related
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.
I make a project of music player in Qt based on QWidget class
and using widget.ui form.
I want to check that user will not add two labels with the same text, so I'm trying to add field: QList labelsList;
in my widget.h file.
(every time user add label, than: labelsList.append(label), and before he can add this label program would iterate through labelsList and check if in list exists label with patricular text).
Although "myqlabel.h" is included, compiler says that 'myQLabel' was not declared in this scope... I don't know why. Kind of weird for me, but maybe I lack/forget some basic knowledge... ;/
Thanks for help!
Code (just needed fragments) below:
widget.h file:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "myqlabel.h"
#include <QList>
#include <QFormLayout>
#include <QSqlDatabase>
#include <QtSql>
#include <QMediaPlayer>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
// to check if label with input text already exists:
// HERE OUR BAD FIELD:
QList<myQLabel*> labelsList;
private:
Ui::Widget *ui;
QMediaPlayer player;
qint64 duration;
};
#endif // WIDGET_H
myqlabel.h file:
#ifndef MYQLABEL_H
#define MYQLABEL_H
#include <QLabel>
#include "widget.h"
#include "ui_widget.h"
#include <QFormLayout>
#include <QMouseEvent>
class myQLabel : public QLabel {
Q_OBJECT
public:
myQLabel(QString& text, QFormLayout* parent = 0) : QLabel(text){
setAcceptDrops(true);
position = amount;
this->parent = parent;
labelsList.append(this);
}
};
#endif // MYQLABEL_H
You have a circular dependency in your includes. This means that one of the headers is not seen depending on the order in which they are first included (due to the include guards).
Remove #include "myqlabel.h" from widget.h and add it to widget.cpp. Then forward declare myQLabel by adding
class myQLabel;
to widget.h.
I am implementing a library management system using Qt C++. I have a Material class which is a QMainwindow and when I click Fiction Section in menu bar Fiction form should be opened which is a QDialogbox. But although I implemented this concept I get the error which is "expected class-name before '{'". Please help to find the error. Thank You in advance.
This is material.h
#ifndef MATERIALS_H
#define MATERIALS_H
#include <QMainWindow>
#include "materialinner.h"
#include "fictionsection.h"
namespace Ui {
class Materials;
}
class Materials : public QMainWindow, public MaterialInner
{
Q_OBJECT
public:
explicit Materials(QWidget *parent = 0);
~Materials();
private slots:
void on_btnAdd_clicked();
void on_btnLoad_clicked();
void on_btnEdit_clicked();
void on_tblMaterial_clicked(const QModelIndex &index);
void on_btnSearch_clicked();
void on_actionClear_triggered();
void createAction();
void on_actionEdit_triggered();
void on_actionDelete_Records_triggered();
void on_actionFiction_section_triggered();
private:
Ui::Materials *ui;
FictionSection *fic;
};
#endif // MATERIALS_H
This is material.cpp
#include "materials.h"
#include "ui_materials.h"
#include <QDebug>
#include <QMessageBox>
Materials::Materials(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Materials)
{
ui->setupUi(this);
// QObject ::connect(ui->lneditSearch,SIGNAL(textChanged(const QString &)),this,SLOT(displaySearch()));
}
Materials::~Materials()
{
delete ui;
}
void Materials::on_actionFiction_section_triggered()
{
/* this->hide();
fiction = new FictionSection();
fiction->show();*/
this->hide();
fic = new FictionSection();
fic->show();
}
This is fictionsection.h
#ifndef FICTIONSECTION_H
#define FICTIONSECTION_H
#include <QDialog>
#include "materials.h"
#include "materialinner.h"
namespace Ui {
class FictionSection;
}
class FictionSection : public QDialog, public Materials
**{**
Q_OBJECT
public:
explicit FictionSection(QWidget *parent = 0);
~FictionSection();
private:
Ui::FictionSection *ui;
};
#endif // FICTIONSECTION_H
Error occurs in functionsection.cpp class. And the curly brace where the error occured is bold.
With the following code snippet it gives the error of "request for memeber 'show' is ambiguous"
Material.cpp
#include "materials.h"
#include "ui_materials.h"
#include "fictionsection.h"
#include <QDebug>
#include <QMessageBox>
Materials::Materials(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Materials)
{
ui->setupUi(this);
// QObject ::connect(ui->lneditSearch,SIGNAL(textChanged(const QString &)),this,SLOT(displaySearch()));
}
void Materials::on_actionFiction_section_triggered()
{
this->hide();
fiction = new FictionSection();
fiction->show();
}
How to solve this?
You have a circular dependency: materials.h includes fictionsection.h and fictionsection.h includes materials.h.
As your header files has routines to prevent multiple inclusion (#ifndef FICTIONSECTION_H and #ifndef MATERIALS_H, which are good), when material.h includes fictionsection.h, this one includes material.h again but this has absolutely no effect due to your multiple inclusion protection....consequence is that fictionsection.h does not get Materials declaration in the end and refuses to declare FictionSection deriving from it!
You need to use a forward declaration to solve that:
In materials.h, replace:
#include "fictionsection.h"
by
class FictionSection;
And add #include "fictionsection.h" in materials.cpp only.
Forward declaration is a common practice to resolve this problem. But, even without this problem occuring, forward declaration remains a good practice because it will speed up your compilation.
Basically I am trying to inherit everything from friction into base (or even the other way round) however, it is not identifying the classes I put in.
base.h
#ifndef BASE_H
#define BASE_H
#include <QMainWindow>
namespace Ui {
class Base;
}
class Base : public QMainWindow{
Q_OBJECT
public:
explicit Base(QWidget *parent = 0);
~Base();
private:
Ui::Base *ui;
};
#endif // BASE_H
friction.h:
#ifndef FRICTION_H
#define FRICTION_H
class Friction : public Base{ // THIS IS WHERE THE ERROR IS
public:
Friction();
};
#endif // FRICTION_H
base.cpp
#include "friction.h"
#include "base.h"
#include "ui_base.h"
Base::Base(QWidget *parent) :QMainWindow(parent),ui(new Ui::Base){
ui->setupUi(this);
}
Base::~Base(){
delete ui;
}
friction.cpp
#include "friction.h"
#include "base.h"
#include "ui_base.h"
Friction::Friction(){
}
and finally main.cpp
int main(int argc, char *argv[]){
QApplication a(argc, argv);
Base w;
w.show();
Friction f;
return a.exec();
}
I receive the error "expected class name before '{' token", I have cut the project down as much as I can and the error still comes up and I really don't know why.
I am fairly new to c++ however I find inheritance not much of an issue on a basic program but upon moving to Qt I couldn't seem to get it working. I have tried numerous things regarding changing the includes etc etc as I am completely oblivious as to why it's not identifying the class.
If friction inherits Base , than you should put:
#include "base.h"
in friction.h file , like so:
#ifndef FRICTION_H
#define FRICTION_H
#include "base.h"
class Friction : public Base{ // THIS IS WHERE THE ERROR IS
public:
Friction();
};
what am I doing wrong?
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "fileoperations.h"
namespace Ui {
class MainWindow;
}
class FileOperations;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
FileOperations FileController;
private slots:
void on_OpenButton_clicked();
void on_SaveButton_clicked();
void on_EncodeButton_clicked();
void on_DecodeButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
When i try to compile and run the program, it says:
g:\ke\c++ projects\projects\qt\shitlencoder\mainwindow.h:18: error: C2079: 'MainWindow::FileController' uses undefined class 'FileOperations'
Here's the strange thing, if I change 'FileOperations FileController;' to 'FileOperations *FileController;'(Obviously this compiles wrongly, because the rest of my codes that you can't see havn't been adapted to '->' instead of '.')
Then if I change it back to 'FileOperations FileController;' it lets me compile the program once (And it works fine), then it has the error the next time I try to compile it.
I'm using Qt 5.0.
fileoperations.h:
#ifndef FILEOPERATIONS_H
#define FILEOPERATIONS_H
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QFileDialog>
#include <string>
#include <time.h>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <fstream>
using namespace std;
class FileOperations
{
public:
FileOperations();
void SetInputFile(QString x);
void SetOutputFile(QString x);
void EncryptAndSave(Ui::MainWindow *NUI);
void DecryptAndSave(Ui::MainWindow *NUI);
void createid(int id, int id2);
int GetCFuncion();
void SetCFuncion(int x);
long long Get_Size(string filename);
bool Get_Toobig(string path);
//DWORD WINAPI Thread_no_1();
private:
string InputFilename;
string OutputFilename;
int CFuncion;//CurrentFunction;
vector<int> conbyte1;
vector<int> conbyte2;
vector<int> opbyte1;
vector<int> opbyte2;
vector<int> passwordbytes;
};
#endif // FILEOPERATIONS_H
I assume that, in your .cpp file, you are using
#include "fileoperations.h"
Then, in fileoperations.h, you are including mainwindow.h which again includes fileoperations.h which is basically correct, since you are using a FileOperations object as parameter. But, due to the guards, class FileOperations is not seen by the compiler this time, hence FileOperations is unknown when used as parameter in your method. You need to break this dependency:
In fileoperations.h, use a forward declaration for Ui::MainWindow and remove the #include "mainwindow.h":
namespace Ui {
class MainWindow;
}
...
Since you are holding a FileOperations object in your class, you need the full class declaration. This means you have to include the header, you cannot simply forward declare the class like you are doing now. If you hold only a pointer, and do not have any code in your header that attempts to dereference the pointer, then the forward declaration is enough.
EDIT You have a cyclical include. You are including mainwindow.h in fileoperations.h. You can fix if by removing that include completely.
You have circular include issue, mainwindow.h and fileoperations.h include each other, try to remove below line from fileoperations.h
#include "mainwindow.h"