我试图将一个对象声明为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;这里是我声明我的extern对象的头文件。为什么我会得到这个错误,我应该如何声明对象,这样我才能在全球范围内使用它们?
#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发布于 2021-11-30 03:43:42
如果我们可以这样做,对于类类型的实例,编译器不知道链接时解析哪个地址。你可以这样做:
//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的意图而不是其成员。
https://stackoverflow.com/questions/70163859
复制相似问题