I am trying to declare an object as extern because I want a thread to be able to access and update it from a different file. But I get the following error message when I try to compile my code:
In file included from main.cpp:1:
dialog.h:43:6: error: storage class specified for ‘temperatureValue’
43 | extern QLabel *temperatureValue;
| ^~~~~~
dialog.h:44:6: error: storage class specified for ‘humidityValue’
44 | extern QLabel *humidityValue;
| ^~~~~~
dialog.h:45:6: error: storage class specified for ‘CO2Value’
45 | extern QLabel *CO2Value;
| ^~~~~~
dialog.h:46:6: error: storage class specified for ‘lightingValue’
46 | extern QLabel *lightingValue;
| ^~~~~~
dialog.h:47:6: error: storage class specified for ‘letterGrade’
47 | extern QLabel *letterGrade;
Here is the header file where I am declaring my extern objects. Why am I getting this error and how should I be declaring the objects so I can use them globally?
#ifndef DIALOG_H
#define DIALOG_H
#include <iostream>
#include <string>
#include <QLabel>
#include <QDialog>
#include <QtWidgets>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QtCore>
#include <QtGui>
#include <QtCharts>
#include <QChart>
#include <QChartView>
#include <QDate>
#include <QTime>
#include <QDateTime>
#include <QFile>
#include <QString>
#include <QStringList>
#include <QTextStream>
#include <QLineSeries>
#include <QIODevice>
#include <QDir>
#include <Qt>
#include <QDateTimeAxis>
#include <QValueAxis>
#include <QComboBox>
#include <QBarSeries>
#include <QFileSystemWatcher>
namespace MainWindow
{
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
// create update graph functions
private slots:
void delayUpdateChart();
void updateChart();
void changePet();
void expandGraph();
private:
//Create the layouts that will make up the GUI
QGridLayout *mainLayout;
QGridLayout *leftLayout;
QVBoxLayout *centerLayout;
QVBoxLayout *rightLayout;
QWidget *leftWidget;
QWidget *centerWidget;
QWidget *rightWidget;
//Create all the labels that will be used to display environmental information
QLabel *temperatureLabel;
QLabel *humidityLabel;
QLabel *CO2Label;
QLabel *lightingLabel;
extern QLabel *temperatureValue;
extern QLabel *humidityValue;
extern QLabel *CO2Value;
extern QLabel *lightingValue;
extern QLabel *letterGrade;
QLabel *petName;
//Create widgets that contain the labels
QWidget *temperatureWidget;
QWidget *humidityWidget;
QWidget *CO2Widget;
QWidget *lightingWidget;
// Create file watcher for data
QFileSystemWatcher *fileWatcher;
// Create chart
QChartView *chartview;
// Create chart type selection
QComboBox *typeComboBox;
QComboBox *rangeComboBox;
// Create pet selection
QComboBox *petSelector;
// Create expand graph check box
QCheckBox *graphCheckBox;
void leftVerticalLayout();
void centerVerticalLayout();
void rightVerticalLayout();
void readLastLine();
// Create helper functions for the graph
QLineSeries* getLineSeries(std::string filename, int typeInd, int rangeInd);
QDateTimeAxis* getDTAxis();
QValueAxis* getValueAxis(std::string label);
};
}
#endif // DIALOG_H
extern a class member is not allowed, If we can do that, for instances of a class type, compiler don't know which address to resolve while linking. you can do like this:
//A.h
#ifndef _A_H_
#define _A_H_
struct A{
int value;
};
extern A a;
#endif
//A.cpp
#include "A.h"
A a;
//main.cpp
#include <iostream>
#include "A.h"
int main() {
std::cout<<a.value<<std::endl;
}
In your codes, extern Dialog's intance instead of its members.