Qt和C++类给了我一个错误
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: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"
- 试图修复一个错误,该错误不会让我开始编程其余部分
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 我正在尝试一个傻瓜 C++ 练习,我遇到了一个错误,说类 'GraduateStudent' 没有任何名为 'advisor' 的字段
- 零四元数和任何向量都不为零的特征积,这是一个错误吗?
- 处理程序的模块列表中有一个错误的模块"WebSocketModule"
- 在 C++ 中使用 "transform" 会给出一个错误,指出这未在作用域中声明
- 在Cython中使用C库时,我遇到了一个错误
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 我试图用c++编写递归fibonacci序列,但当我编译时,我遇到了一个错误
- 从system()调用G++会返回一个错误
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- 我收到一个错误无效的操作数,类型为 const char [42] 和二进制"运算符+"的双倍数
- 使用声明:GCC 和 Clang 的另一个错误?
- 全球免费给出一个错误.调试器不解释
- 相对于继承的构造函数,gcc 编译器是否还有一个错误?
- 为什么直接传递"this"指针来存档是一个错误,而另一个相同类型的指针是可以的?
- 为什么第三板有一个错误
- 如何编写一个错误结构,该结构可以包含不同的强键枚举作为错误代码
- 使用本地类型声明的G lambda被使用但从未定义 - 确实是一个错误
- 我需要在 Android Studio 中构建 NDK. 但它返回一个错误