将对象声明为外部

Declaring an object as extern

我正在尝试将对象声明为外部对象,因为我希望线程能够从不同的文件访问和更新它。但是当我尝试编译我的代码时收到以下错误消息:

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;

这是我声明外部对象的头文件。为什么我会收到此错误以及我应该如何声明对象以便我可以全局使用它们?

#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 成员是不允许的,如果我们可以这样做,对于 class 类型的实例,编译器在链接时不知道要解析哪个地址。你可以这样做:

//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;   
}

在您的代码中,extern Dialog 的实例而不是其成员。