undeclared identifier error with correct include and declaration - c++

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.

Related

Downloading txt file with qtnetwork

I am trying to download a txt file from a url in QT but i can't seem to make it work.
I am following this guide https://wiki.qt.io/Download_Data_from_URL. I implemented the filedownloader class exactly like it's made in the guide, but when i try to use it like specified in the guide I cannot make it work. I created a slot to be called when the download is finished, but if i try to call the downloader inside like the guide it says it is an undeclared identifier.
Does anyone know how to correctly implement this code?
this is the .cpp of my code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QStringList>
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QList>
#include <QtNetwork/QNetworkReply>
#include <QStringList>
#include <QTimer>
#include <QUrl>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <filedownloader.h>
#include <iostream>
#include <QObject>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QUrl emiurl( "url of my file");
// call to the downloader class.In the guide it's made differently, but it doesn't work
FileDownloader emiload(emiurl,this);
//this connect links the end of the download with the textwriter slot
QObject::connect(&emiload, SIGNAL (downloaded()), this, SLOT (textwriter()));
}
>MainWindow::~MainWindow()
{
delete ui;
}
//slot needed to create the txt file from the downloaded one
void MainWindow::textwriter()
{
QByteArray emibyte;
emibyte=emiload->downloadedData(); //this line gives me error
QFile emifile("emi.txt");
emifile.open(QIODevice::WriteOnly);
std::cout << emibyte.size() << std::endl;
QDataStream out(&emifile);
out << emibyte;
std::cout << emifile.size() << std::endl;
}
Now here's the .h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "filedownloader.h"
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void textwriter();
};
#endif // MAINWINDOW_H
To make the undeclared identifier go away and successfully compile, you need to add FileDownloader to the class' declaration. This way, it will be known throughout the class.
I chose to go with the approach that's usual in Qt, to declare a pointer to FileDownloader.
#pragma once // <--- this is supported by virtually any compiler today
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class FileDownloader; // <-- forward declaration is enough, but you can also #include "filedownloader.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr);
~MainWindow();
private:
Ui::MainWindow* ui = nullptr;
FileDownloader* emiload = nullptr; // <--- the important line!
private slots:
void textwriter();
};
And then instantiate and call emiload in the constructor:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// create an instance of FileDownloader with "new".
emiload = new FileDownloader(QUrl("url of my file"), this);
// using member pointer connection available since Qt5
connect(emiload, &FileDownloader::downloaded, this, &MainWindow::textwriter);
}

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.

Signals and slots with std::vector : lvalue issue

In one of my project, I am trying to use the Qt signals and slots mechanism with a std::vector<bool> as parameter. My project is equivalent to the following minimal code :
class App
// app.h
#ifndef APP_H
#define APP_H
#include <QObject>
#include <QSharedPointer>
#include "emitter.h"
#include "receiver.h"
class App : public QObject
{
Q_OBJECT
public:
App(QObject *parent = 0);
};
#endif // APP_H
// app.cpp
#include "app.h"
App::App(QObject* parent): QObject(parent)
{
Emitter emitter;
Receiver receiver;
receiver.attachEmitter(emitter);
emitter.run();
}
class Emitter
//emitter.h
#ifndef EMITTER_H
#define EMITTER_H
#include <QObject>
#include <vector>
#include "helper.h"
class Helper;
class Emitter : public QObject
{
Q_OBJECT
public:
explicit Emitter(QObject *parent = 0);
void run();
signals:
void triggered(std::vector<bool> value);
};
#endif // EMITTER_H
// emitter.cpp
#include "emitter.h"
Emitter::Emitter(QObject *parent) : QObject(parent)
{
}
void Emitter::run()
{
emit triggered(Helper::value());
}
class Receiver
//receiver.h
#ifndef RECEIVER_H
#define RECEIVER_H
#include <QObject>
#include <QDebug>
#include <QSharedPointer>
#include "emitter.h"
class Emitter;
class Receiver : public QObject
{
Q_OBJECT
public:
explicit Receiver(QObject *parent = 0);
void attachEmitter(QSharedPointer<Emitter> emitter);
signals:
public slots:
};
//receiver.cpp
#include "receiver.h"
Receiver::Receiver(QObject *parent) : QObject(parent)
{
}
void Receiver::attachEmitter(QSharedPointer<Emitter> emitter)
{
connect(emitter.data(), &Emitter::triggered, [this](std::vector<bool> value) {
qDebug() << "Received value";
});
}
For some reason, the compiler doesn't like it at all and prints me this stack :
What do I have to do ? Thank you
Signal/slot values passend by value have to be registered with the Qt meta system and I don't think the std::vector is registered by default. Is there a reason you're not using QVectorinstead?
For futher information look at Q_DECLARE_METATYPE and qRegisterMetaType().

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.

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

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;