QT GUI 메인 윈도우 내에서 "Fan"클래스에서 x라는 객체를 만들려고합니다. 여기서 전역 객체가되도록하고 싶습니다. QT의 버튼 슬롯 기능이 객체에 대한 작업을 수행 할 수 있기를 바랍니다. 그러나 컴파일러 오류 "오류 : C4430 : 누락 된 형식 지정자 - int 가정"항상 발생합니다. 여기에 헤더 파일입니다QT GUI에서 전역 객체 만들기
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_btOn_clicked();
void on_btOff_clicked();
private:
Ui::MainWindow *ui;
Fan x; // This doesn't work
Fan * x; // This doesn't either
int x; // This does work
};
#endif // MAINWINDOW_H
그리고 여기 CPP 파일입니다
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "fan.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btOn_clicked()
{
ui->lblState->setText("Fan is on");
}
void MainWindow::on_btOff_clicked()
{
x.turnOff(); // This does not work of course
x->turnOff(); // Or this
ui->lblState->setText("Fan is off");
}
내가 이미 fan.h 파일에서 팬 클래스를 포함하는 CPP 파일을 말했다. 창 생성자 내에서 개체를 만들면 잘 초기화되지만 전역이 아닙니다. 또한 헤더 파일을 순환 포함하지 않습니다. 팬 클래스는 메인 윈도우를 포함하지 않습니다.
아마도 내가 검색하는 방법을 모르겠지만, 이미 그것에 대한 조사를 해본 결과 아무 소용이 없습니다. 어떤 도움을 주셔서 감사합니다.
편집
:#ifndef FAN_H
#define FAN_H
class Fan
{
private:
int speed;
bool isOn;
public:
Fan();
void setSpeed(int);
void turnOn();
void turnOff();
int getSpeed();
bool getState();
};
#endif // FAN_H
에
#include
Fan.h
to이를 사용하는 경우 'MainWindow' 클래스 선언. – vahancho
감사합니다. 난 그저 시도했지만, 지금은 "해결되지 않은 외부 기호"오류 on_btOn_clicked() 및 on_btOff_clicked() 함수는 특히 x.turnOn() 및 x.turnOff() 줄에서 오류가 발생합니다. 왜 이런거야? –
@AllanVindel :'Fan' 클래스 (.cpp)를 컴파일하지 않았거나'Fan :: turnOn()'함수에 정의가 없습니다. – vahancho