[英]Qt and C++ class giving me an error
我究竟做錯了什么?
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "fileoperations.h"
namespace Ui {
class MainWindow;
}
class FileOperations;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
FileOperations FileController;
private slots:
void on_OpenButton_clicked();
void on_SaveButton_clicked();
void on_EncodeButton_clicked();
void on_DecodeButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
當我嘗試編譯並運行程序時,它說:
g:\ke\c++ projects\projects\qt\shitlencoder\mainwindow.h:18: error: C2079: 'MainWindow::FileController' uses undefined class 'FileOperations'
如果更改“ FileOperations FileController;”,這就是奇怪的事情。 到'FileOperations * FileController;'(顯然這編譯錯誤,因為我看不到的其余代碼都沒有被改編成'->'而不是'。')然后如果我將其改回'FileOperations FileController;' 它讓我只編譯一次程序(並且工作正常),然后在下次嘗試編譯時出現錯誤。
我正在使用Qt 5.0。
fileoperations.h:
#ifndef FILEOPERATIONS_H
#define FILEOPERATIONS_H
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QFileDialog>
#include <string>
#include <time.h>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <fstream>
using namespace std;
class FileOperations
{
public:
FileOperations();
void SetInputFile(QString x);
void SetOutputFile(QString x);
void EncryptAndSave(Ui::MainWindow *NUI);
void DecryptAndSave(Ui::MainWindow *NUI);
void createid(int id, int id2);
int GetCFuncion();
void SetCFuncion(int x);
long long Get_Size(string filename);
bool Get_Toobig(string path);
//DWORD WINAPI Thread_no_1();
private:
string InputFilename;
string OutputFilename;
int CFuncion;//CurrentFunction;
vector<int> conbyte1;
vector<int> conbyte2;
vector<int> opbyte1;
vector<int> opbyte2;
vector<int> passwordbytes;
};
#endif // FILEOPERATIONS_H
我假設在您的.cpp文件中,您正在使用
#include "fileoperations.h"
然后,在fileoperations.h
,包含mainwindow.h
,該文件又包含基本上正確的fileoperations.h
,因為您使用FileOperations
對象作為參數。 但是,由於有保護措施,這次編譯器無法看到class FileOperations
,因此在您的方法FileOperations
用作參數時是未知的。 您需要打破這種依賴性:
在fileoperations.h
,對Ui::MainWindow
使用前向聲明,並刪除#include "mainwindow.h"
:
namespace Ui {
class MainWindow;
}
...
由於您要在類中保存FileOperations
對象,因此需要完整的類聲明。 這意味着您必須包含標頭,不能像現在一樣簡單地向前聲明該類。 如果僅持有一個指針,並且標頭中沒有任何試圖取消引用該指針的代碼,則前向聲明就足夠了。
編輯您有一個周期性的包含。 您將mainwindow.h
包含在fileoperations.h
。 您可以通過完全刪除該包含項來解決。
您有循環包含問題,mainwindow.h和fileoperations.h相互包含,請嘗試從fileoperations.h
刪除以下行
#include "mainwindow.h"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.