首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将对象声明为extern

将对象声明为extern
EN

Stack Overflow用户
提问于 2021-11-30 03:15:44
回答 1查看 165关注 0票数 0

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

代码语言:javascript
复制
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;

这里是我声明我的extern对象的头文件。为什么我会得到这个错误,我应该如何声明对象,这样我才能在全球范围内使用它们?

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-30 03:43:42

如果我们可以这样做,对于类类型的实例,编译器不知道链接时解析哪个地址。你可以这样做:

代码语言:javascript
复制
//A.h
#ifndef _A_H_
#define _A_H_
struct A{
    int value;
    
};
extern A a;
#endif
代码语言:javascript
复制
//A.cpp
#include "A.h"
A a;
代码语言:javascript
复制
//main.cpp
#include <iostream>

#include "A.h"
int main() {

std::cout<<a.value<<std::endl;   
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70163859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档