gpt4 book ai didi

c++ - "undefined class"Qt/C++

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:11 25 4
gpt4 key购买 nike

我最近开始使用 C++ 编程,并且在类之间的数据交换方面遇到了一些困难。我搜索了许多教程和示例,但没有一个有效。我最终决定寻求帮助,因为我不知道该怎么做......

PS: 我想从我的 B (solEditor) 类调用 keyPressEvent 函数

谢谢。

addons/soleditor.h(类)

#include <Qsci/qsciscintilla.h>
#include <QWidget>
#include <QKeyEvent>

class Editor; // main class
class solEditor : public QsciScintilla
{

public:
explicit solEditor(QWidget *parent = 0);
Editor editor; // error <<

protected:
void keyPressEvent(QKeyEvent *e);

};

editor.h(主窗口)

#include <QMainWindow>
#include "about.h"
#include "data.h"
#include "addons/codeeditor.h"
#include "addons/highlighter.h"
#include "addons/soleditor.h"
#include "plugins/headergenerator.h"
#include "plugins/updatescreen.h"

namespace Ui {
class Editor;
}

class solEditor;

class Editor : public QMainWindow
{

Q_OBJECT

solEditor *sE;

public:
QsciScintilla* ce;
Highlighter *Hl;
solEditor *e;
Ui::Editor *ui;
explicit Editor(QWidget *parent = 0);
~Editor();

public slots:
void on_KeyPress(QKeyEvent *e);

};

错误:

SOLEditor\addons\soleditor.h:14: error: C2079: 'solEditor::editor' uses undefined class 'Editor'

最佳答案

您正在使用前向声明

class Editor; // main class

使用该类的本地实例

Editor editor; // error <<

不幸的是,这是不可能的,因为在这种情况下,编译器需要知道 Editor 类的全部细节。有两种可能的解决方案:

  1. 使成员成为指针:Editor* editor
  2. 使用 #include 而不是前向声明。

如果你想让你的代码更灵活,你可以这样做:

class solEditor : public QsciScintilla
{

public:
explicit solEditor(Editor* editor, QWidget* parent = 0);

inline Editor* editor() const { return _editor; }

private:
Editor* _editor;
}

在cpp文件中:

solEditor::solEditor(Editor* editor, QWidget* parent)
// TODO: call parent constructor
{
_editor = editor;
}

solEditor::~solEditor()
{
_editor = NULL;
}

然后您可以像这样创建一个 solEditor 实例:

QWidget* optionalParentWidget = GetParentWidgetFromSomewhere();
Editor* editor = GetEditorInstanceFromSomewhere(); // or create it yourself

solEditor* myEditor = new solEditor(editor, optionalParentWidget);

(作为辅助节点,C++ 中的类通常以大写字母开头:QWidget、Editor、SolEditor。这样可以更容易地识别某个东西是类、函数还是变量) .


关于按键事件:如果想让一个类处理另一个类的某些事件,最好使用eventFilter机制:

http://qt-project.org/doc/qt-5/qobject.html#installEventFilter

关于c++ - "undefined class"Qt/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25858809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com