Declaring my object as 'extern' gives "storage class specified" error - c++

I have 3 classes - GLWidget, Window and rules. Window creates an object of GLWidget called 'm_glWidget' which I am trying to access from the class rules. I have tried using 'extern' and changing things up but nothing seems to make it work. The closest I have got is a single error which says "storage class specified for 'm_glWidget'" but I am unsure on what this means. This is my code:
Window.h
#include "glwidget.h"
#include "rules.h"
class GLWidget; class rules;
class Window : public QDialog, public Ui::frmMain
{
Q_OBJECT;
public:
Window(QWidget *parent = 0);
rules *gameRules;
GLWidget *m_glWidget;
Window.cpp
#include "rules.h"
#include "glwidget.h"
class rules; class GLWidget;
using namespace std;
Window::Window(QWidget *parent):QDialog(parent)
{
m_glWidget = new GLWidget;
gameRules = new rules;
rules.h
#include "windows.h"
#include "glwidget.h"
class rules{
public:
rules();
extern GLWidget *m_glWidget;
rules.cpp
#include "window.h"
#include "glwidget.h"
using namespace std;
rules::rules(){
}
void rules:: print(){
cout << m_glWidget->x << endl;
}

How about this?
#include "windows.h"
#include "glwidget.h"
class rules{
public:
rules(GLWidget *glWidget)
: m_glWidget(glWidget)
{
}
GLWidget *m_glWidget;

Related

C++ Qt4.8 :: Pass Object to another Class - member access into incomplete type error

I am new in C++ Qt and struggling with the correct use of forward declarations and #include.
What I want to do:
I have a Qt Gui (Class Ui::Gui) where we can set values.
I want to save these values in Gui Class variables.
As soon as a button (Generate Xml) is clicked, I want to pass the object
'ui' to the XmlGeneratorClass, So i can use the values to generate a Xml.
gui.h
#ifndef GUI_H
#define GUI_H
#include <QMainWindow>
#include <QDebug>
#include "xmlgeneratorqobject.h"
namespace Ui {
class Gui;
}
class Gui : public QMainWindow
{
Q_OBJECT
public:
explicit Gui(QWidget *parent = nullptr);
~Gui();
qint8 testvalue = 1;
signals:
void transmitToXmlGen(Ui::Gui*);
private slots:
void on_pushButtonGenerateXml_clicked();
private:
Ui::Gui *ui;
XmlGeneratorQObject *xmlgenerator = new XmlGeneratorQObject();
};
#endif // GUI_H
gui.cpp
#include "gui.h"
#include "ui_gui.h"
Gui::Gui(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Gui)
{
ui->setupUi(this);
connect(this,SIGNAL(transmitToXmlGen(Ui::Gui*)),xmlgenerator,SLOT(receiveFromGui(Ui::Gui*)));
}
Gui::~Gui()
{
delete ui;
}
void Gui::on_pushButtonGenerateXml_clicked()
{
emit transmitToXmlGen(ui);
}
xmlgeneratorqobject.h
#ifndef XMLGENERATORQOBJECT_H
#define XMLGENERATORQOBJECT_H
#include <QObject>
#include <QDebug>
namespace Ui {
class XmlGeneratorQObject;
class Gui;
}
class XmlGeneratorQObject : public QObject {
Q_OBJECT
public:
explicit XmlGeneratorQObject(QObject * parent = nullptr);
private slots:
void receiveFromGui(Ui::Gui*);
};
#endif // XMLGENERATORQOBJECT_H
xmlgeneratorqobject.cpp
#include "xmlgeneratorqobject.h"
XmlGeneratorQObject::XmlGeneratorQObject(QObject *parent){}
void XmlGeneratorQObject::receiveFromGui(Ui::Gui* objectFromGui)
{
qDebug() << objectFromGui->testvalue; // ERROR member access into incomplete type 'Ui::Gui'
}
Expected result:
Access to public variables from passed gui-object should be possible
Actual result:
member access into incomplete type 'Ui::Gui'
Can you please help me learn forward declaration / include?
Is my approach in general okay?
Your xmlgeneratorqobject.cpp needs the line
#include "ui_gui.h"
This gives it the details of the ui widgets. This file is generated by the Qt build system.

How to solve expected class-name before '{' in QT c++

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.

Object creation and adding to a list

Object creation: I want to create an object of class Note, that is a QWidget. How can I do this? I am only able to show the Note bla that has been declared as a private member of class Traymenu. This goes by clicking on "new Note" in the context Menu of the trayMenu. I want to create a new note for each click on this and add it to a list of Notes in trayMenu to keep the note alive, but don't have an idea how to define a list of elements of objects of my own class Note.
So my 2 problems are: How to create a new note on runtime and how to keep it alive in a list that belongs to trayMenu.
If i comment out the 2 lines Note nwNtand nwNt.show() the compiler says that something in qglobals.h is private.
traymenu.h:
#ifndef TRAYMENU_H
#define TRAYMENU_H
#include <QSystemTrayIcon>
#include <QIcon>
#include <QPixmap>
#include <QMenu> //in use for context menu
#include "note.h"
class Traymenu : public QSystemTrayIcon
{
public:
Traymenu();
~Traymenu();
void createMainContextMenu();
void newNote();
private:
QSystemTrayIcon mainIcon;
QMenu mainContextMenu;
Note bla;
};
#endif // TRAYMENU_H
traymenu.cpp:
#include "traymenu.h"
#include <QDebug>
Traymenu::Traymenu(){
mainIcon.setIcon(QIcon(QPixmap("C:\\papersavers.png")));
mainIcon.setVisible(true);
mainIcon.show();
createMainContextMenu();
}
Traymenu::~Traymenu(){
}
void Traymenu::createMainContextMenu(){
QAction *actionNewNote = mainContextMenu.addAction("Neue Notiz");
QObject::connect(actionNewNote,&QAction::triggered,this,&Traymenu::newNote);
mainIcon.setContextMenu(&mainContextMenu);
}
void Traymenu::newNote(){
qDebug() << "in newNote()" << endl;
//Note nwNt;
//nwNt.show();
bla.show()
}
note.h:
#ifndef NOTE_H
#define NOTE_H
#include <QWidget>
namespace Ui{
class Note;
}
class Note : public QWidget
{
public:
Note(QWidget *parent = 0);
~Note();
private:
Q_OBJECT
Ui::Note *ui;
};
#endif // NOTE_H
note.cpp:
#include "note.h"
#include "ui_note.h"
Note::Note(QWidget *parent) :
QWidget(parent),
ui(new Ui::Note)
{
ui->setupUi(this);
}
Note::~Note()
{
delete ui;
}

undeclared identifier error with correct include and declaration

I'm creating an application in Qt
i get a compilation error: error C2065: 'callDialog' : undeclared identifier
in my CategoryDialog class
error line:
CallDialog* callDialog = ((CallDialog*)dialogStack->widget(1));
5th last line in CategoryDialog class:
#include "ui_categorydialog_new.h"
#include "calldialog.h"
#include "questionsdialog.h"
#include "../objects/call.h"
#include "../webservice/dataconnector.h"
#include "../webservice/dataconngetter.h"
namespace Ui {
class CategoryDialog;
}
class CategoryDialog : public QDialog
{
Q_OBJECT
public:
explicit CategoryDialog(QWidget *parent = 0) : QDialog(parent), ui(new Ui::CategoryDialog){
ui->setupUi(this);
}
~CategoryDialog()
{
delete ui;
}
private slots:
void on_btn_back_clicked()
{
ui->btn_back->setEnabled(false);
DataConnGetter::getConnector()->setCallAbort(call->getId()); //get errormessage back and show errormessage when nessesary
QStackedWidget* dialogStack = (QStackedWidget*)this->parentWidget();
CallDialog* callDialog = ((CallDialog*)dialogStack->widget(1)); //TODO 005 replace indexes with enums > more clear
callDialog->updateCalls(false);
dialogStack->setCurrentIndex(1);
ui->btn_back->setEnabled(true);
}
the CallDialog class looks like this
#include <QDialog>
#include <QString>
#include <QList>
#include <QSound>
#include <QtDebug>
#include <QStringList>
#include <QPushButton>
#include <QStackedWidget>
#include <QtAlgorithms>
#include <QLabel>
#include <typeinfo>
#include "ui_calldialog.h"
#include "callbutton.h"
#include "categorydialog_new.h"
#include "../settings/consts.h"
#include "../webservice/dataconnector.h"
#include "../webservice/dataconngetter.h"
#include "../settings/programsettings.h"
#include "../webservice/pollingservice.h"
class PollingService;
namespace Ui {
class CallDialog;
}
class CallDialog : public QDialog
{
Q_OBJECT
public:
// explicit CallDialog(QWidget *parent = 0);
// ~CallDialog();
// void initCalls();
// void updateCalls(bool sound);
// void enablePoller();
explicit CallDialog(QWidget *parent = 0) : QDialog(parent), ui(new Ui::CallDialog)
{
ui->setupUi(this);
installEventFilter(this);
notice the correct include in the CategoryDialog
they do include each others (maybe cyclic dependency problem?)
I tried with forward declaring CallDialog. didn't help.
the files are only the .h files with implementation directly inside
EDIT
I bypassed the problem as follows:
i added an abstract class with the function that CallDialog used from CategoryDialog
like this:
#ifndef ABSTRACTDIALOG_H
#define ABSTRACTDIALOG_H
#include <QDialog>
#include "../objects/call.h"
class AbstractDialog : public QDialog
{
Q_OBJECT
public:
explicit AbstractDialog(QWidget *parent = 0) : QDialog(parent){}
void setCall(Call* call){
_call = call;
}
private:
Call* _call;
};
#endif // ABSTRACTDIALOG_H
Forward declaring CallDialog before CategoryDialog won't work since CallDialog member functions are used and the compiler needs to know about them.
Did you try forward declaring CategoryDialog in before CallDialog and remove, I'm guessing, #include "categorydialog_new.h"? Since the all of calldialog.h is not shown, I'm not sure how you are using CategoryDialog in that file.

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.