Qt和C++类给了我一个错误

Qt and C++ class giving me an error

本文关键字:一个 错误 C++ Qt      更新时间:2023-10-16

我做错了什么?

#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:kec++ projectsprojectsqtshitlencodermainwindow.h:18: error: C2079: 'MainWindow::FileController' uses undefined class 'FileOperations'
奇怪的

是,如果我将"FileOperations FileController"更改为"FileOperations *FileController";(显然这是错误的,因为我看不到的其余代码没有适应"->"而不是".")然后,如果我将其改回"FileOperations FileController";它允许我编译程序一次(并且它工作正常),那么下次我尝试编译它时它就会出错。

我正在使用Qt 5.0。

文件操作.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 对象,因此需要完整的类声明。这意味着你必须包含标头,你不能像现在这样简单地转发声明类。如果只保存一个指针,并且标头中没有任何尝试取消引用指针的代码,则前向声明就足够了。

编辑 您有一个周期性包含。您在fileoperations.h中包含mainwindow.h。您可以通过完全删除该包含来修复。

你有循环包含问题,mainwindow.h 和 fileoperations.h 相互包含,尝试从fileoperations.h中删除以下行

#include "mainwindow.h"
相关文章: