Issue with inheritance in Qt - c++

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();
};

Related

Qt: how to call main window's pointer from another class?

I want to separate my roles in a game to different .cpp and .h files,
but I have trouble calling the pointer in mainwindow from another class, which I added through scene->add() in mainwindow.cpp.
It shows "player" was not declared in this scope when I run.
I know this question seems pretty basic, but I've tried my best to fix it with no results.
The following are main methods I've tried:
add MainWindow:: before player, but it shows the similar error
that mainwindow is not declared in this scope.
revise player to this pointer, and it says that class Player has no such member.
revise player to pos() pointer, it says pos() is not
declared in this scope
set class Player as friend with main window, but nothing changes
Here is the code:
main window.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
scene(new QGraphicsScene(0,0,800,600)),
timer(new QTimer)
{
ui->setupUi(this);
ui->graphicsView->setScene(scene);
player = new QGraphicsPixmapItem(QPixmap(":/img/whitedog.png").scaled(150,150));
scene->addItem(player);
player->setPos(0, 0);
timer->start(10);
}
MainWindow::~MainWindow()
{
delete ui;
}
main window.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QTimer>
#include <QKeyEvent>
#include "player.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
friend class Player;
~MainWindow();
private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
QTimer *timer;
QGraphicsItem *player;
};
#endif // MAINWINDOW_H
player.cpp
#include "player.h"
Player::Player()
{
}
void Player::keyPressEvent(QKeyEvent *e)
{
switch(e->key()) {
case Qt::Key_Up:
player->setPos(player->x(), player->y()-10);
break;
}
}
player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <QKeyEvent>
#include <QGraphicsScene>
#include <QMainWindow>
#include <QGraphicsPixmapItem>
#include <QTimer>
#include "ui_mainwindow.h"
class Player
{
public:
Player();
void keyPressEvent(QKeyEvent *e);
};
#endif // PLAYER_H
You are getting confused between two different things.
You have a class Player, which I believe is the one that you want to declare in mainwindow.h but an object of class Player is not created anywhere( as far as what is shown above ).
The player currently declared in the mainwindow.h is of type QGraphicsItem and not of the Player class that you created, so you cannot expect your keyPressEvent to work on it.
You need to have a second look at your design, know the purpose of the Player class, it's responsibilities. Currently it is a simple class not inheriting from anything, did you want it to maybe inherit from QGraphicsItem.
In such a case, your class maybe could look something like this :
class Player : public QGraphicsItem
{
//Players's responsibilities.
}
And in your mainwindow.h, you can just use the instance of the Player instead of using a QGraphicsItem instance.
class MainWindow : public QMainWindow
{
...// other stuff
private:
Player *player;
}
Additional Pointers :
1 . I don't see a reason for the Player class to be a friend class in mainwindow.h, but then again it is your design.
2 . Prefer initializing the player object part in the member initialization list rather than doing it in the constructor.

Error while declaring QVector of class type

Getting error while declaring QVector of class type in Qt.
Error :"incomplete type is not allowed"
I didn't understand what causes this error.
if i #include "storage.h" in main.cpp and declare a Qvector of class storeage it works fine but when i do the same in waveform class it reports an error.
I've tried forward declaring storage class in waveform class but still getting the same error.
Any Help??
main.cpp
#include "test_subject_02.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TEST_SUBJECT_02 w;
w.show();
return a.exec();
}
test_subject_02.h
#ifndef TEST_SUBJECT_02_H
#define TEST_SUBJECT_02_H
#include <QtGui/QMainWindow>
#include "ui_test_subject_02.h"
#include"waveform.h"
#include "storage.h"
class TEST_SUBJECT_02 : public QMainWindow
{
Q_OBJECT
public:
TEST_SUBJECT_02(QWidget *parent = 0, Qt::WFlags flags = 0);
waveform *wv;
~TEST_SUBJECT_02();
private:
Ui::TEST_SUBJECT_02Class ui;
};
#endif // TEST_SUBJECT_02_H
test_subject_02.cpp
#include "test_subject_02.h"
TEST_SUBJECT_02::TEST_SUBJECT_02(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QVector<storage> ser; //works fine here
wv->readfile("e:/testing2/result/3/abc.cur");
}
TEST_SUBJECT_02::~TEST_SUBJECT_02()
{
}
waveform.h
#ifndef WAVEFORM_H
#define WAVEFORM_H
#include "storage.h"
#include <QObject>
class waveform : public QObject
{
Q_OBJECT
public:
waveform(QObject *parent=0);
void readfile(QString);
QVector <storage> myvector ; // incomplete type is not allowed
~waveform();
private:
};
#endif // WAVEFORM_H
waveform.cpp
#include "waveform.h"
waveform::waveform(QObject *parent)
: QObject(parent)
{
}
void waveform::readfile(QString file)
{
QVector<storage> sw; //again error incomplete type is not allowed
}
waveform::~waveform()
{
}
storage.h
#ifndef STORAGE_H
#define STORAGE_H
#include <QObject>
class storage : public QObject
{
Q_OBJECT
public:
storage(QObject *parent);
storage();
storage(QString,QString);
~storage();
private:
QString x;
QString y;
};
#endif // STORAGE_H
storage.cpp
#include "storage.h"
storage::storage(QObject *parent)
: QObject(parent)
{
}
storage::storage(QString xcord,QString ycord)
{
x=xcord;
y=ycord;
}
storage::storage()
{
}
storage::~storage()
{
}
You need to explicitly include QVector in necessary file (waveform.h i believe), since QT uses a lot of forward declarations, while they appear as correct classes in IDE, but they won't compile, if proper definition is not included in file.

how to use objects of the widgets in QT

In my application I'm having 2 widgets named as widget and form. But if i try to create a pointer object of widget widget in widget form header file , it is giving the error like "Form does not name a type". Refer my used code below:
main.cpp
#include <QtGui/QApplication>
#include "widget.h"
#include "form.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *w = new Widget();
w->show();
return a.exec();
}
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include "ui_widget.h"
#include "form.h"
class Widget : public QWidget, private Ui::Widget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
Form *f ;//i try to create pointer object for Form
};
#endif // WIDGET_H
form.cpp
#include "form.h"
#include "widget.h"
Form::Form(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
form.h
#ifndef FORM_H
#define FORM_H
#include "ui_form.h"
#include "widget.h"
class Form : public QWidget, private Ui::Form
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
};
What I'm doing wrong?
The problem is that widget.h includes form.h, which includes widget.h. The header guards (#ifndef) cause the second include to be skipped.
For declaring a pointer variable in a header a forward declaration will suffice:
SomeClass.h
class Form; // forward declaration
class SomeClass {
public:
SomeClass();
// ...
private:
Form* form; // pointer to Form
};
SomeClass.cpp
SomeClass::SomeClass()
{
form = new Form();
}
You should put a forward declaration of class Form in widget.h instead of #includeing form.h. The problem is that you include form.h, which includes widget.h, which tries to include form.h, but can't because of the include guard. Therefore, in widget.h, class Form is undefined, although it looks to the user to be defined.

C++: Problems with having a containing class call a function from a contained class

I believe this might be an issue of #including or forward declaring, rather than an issue of my syntax, since I'm currently getting errors of "invalid use of incomplete type 'struct MainWindow'", and "forward declaration of 'struct MainWindow' when I attempt to build the following in Qt Creator (Qt 4.7.4):
MYCLASS.H
#ifndef MYCLASS_H
#define MYCLASS_H
class MainWindow;
class MyClass
{
public:
MyClass(MainWindow * parent);
void callParentFunction();
private:
MainWindow *myPointer;
};
#endif // MYCLASS_H
MYCLASS.CPP
#include "myclass.h"
MyClass::MyClass(MainWindow *parent) : myPointer(parent)
{
}
void MyClass::callParentFunction()
{
myPointer->setSpinBoxValue(500);
}
MAINWINDOW.H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDoubleSpinBox>
#include "myClass.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
void setSpinBoxValue(double x);
private:
QDoubleSpinBox *mySpinBox;
MyClass *myObject;
};
#endif // MAINWINDOW_H
MAINWINDOW.CPP
#include "mainwindow.h"
MainWindow::MainWindow()
{
mySpinBox = new QDoubleSpinBox;
setCentralWidget(mySpinBox);
myObject = new MyClass(this);
myObject->callParentFunction();
}
void MainWindow::setSpinBoxValue(double x)
{
mySpinBox->setValue(x);
}
I'd appreciate any ideas. Thanks!
You need to include mainwindow.h in myclass.cpp after myclass.h. In myclass.cpp you call a method of the MyClass (inside MyClass::callParentFunction), but at that point the compiler still only has the forward-declaration of MainWindow.

expected class-name before '{' token

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.